0

是否可以在 Android 的对话框中打开一个类?

例如 :

Dialog settingsDialog = new Dialog(this);
        settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        settingsDialog.setContentView(getLayoutInflater().
                                   inflate(R.layout.image_pager, null));
        settingsDialog.show();

正在打开图像。我怎样才能为加载类而不是 xml 布局做同样的事情?

附加信息:
这是我的 ImagePager 类

public class ImagePopUp extends BaseActivity implements OnClickListener {

    private ViewPager pager;
    Button boutton_retour;
    public static String img;


    private DisplayImageOptions options;
    String url,image,title;
    ArrayList<Post> PostDetails = new ArrayList<Post>();

    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_diaporama_pager);

        showViewHolderExtra();

    }


    private class ImagePagerAdapter extends PagerAdapter {

        private String[] images;
        private LayoutInflater inflater;

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }

        ImagePagerAdapter(String[] images) {
            this.images = images;
            inflater = getLayoutInflater();
        }

        @Override
        public void finishUpdate(View container) {
        }

        @Override
        public int getCount() {
            return images.length;
        }


        @Override
        public Object instantiateItem(View view, int position) {

            Dialog settingsDialog = new Dialog(ImagePopUp.this);
            settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.image_layout, null));
            settingsDialog.show();

            final View imageLayout = inflater.inflate(R.layout.item_pager_image, null);
            final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.im_pager);
            final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loadinprogress);

            imageLoader.displayImage(images[position], imageView, options, new ImageLoadingListener() {

                public void onLoadingStarted() {
                    spinner.setVisibility(View.VISIBLE);

                }


                public void onLoadingFailed(FailReason failReason) {
                    String message = null;
                    switch (failReason) {
                        case IO_ERROR:
                            message = "Input/Output error";
                            break;
                        case OUT_OF_MEMORY:
                            message = "Out Of Memory error";
                            break;
                        case UNKNOWN:
                            message = "Unknown error";
                            break;
                    }

                    Toast.makeText(ImagePopUp.this, message, Toast.LENGTH_SHORT).show();

                    spinner.setVisibility(View.GONE);
                    imageView.setImageResource(android.R.drawable.ic_delete);
                }

                public void onLoadingComplete(Bitmap loadedImage) {
                    spinner.setVisibility(View.GONE);
                    Animation anim = AnimationUtils.loadAnimation(ImagePopUp.this, R.anim.fade_in);
                    imageView.setAnimation(anim);
                    anim.start();
                }

                public void onLoadingCancelled() {
                    // Do nothing
                }
            });

            ((ViewPager) view).addView(imageLayout, 0);
            return imageLayout;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View container) {
        }

    }

    public void showViewHolderExtra(){

        String[] seperated = img.split(";");

        Bundle bundle = getIntent().getExtras();
        int pagerPosition = bundle.getInt("POSITION", 0);

        System.out.println("image: "+ pagerPosition);

        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new ImagePagerAdapter(seperated));
        pager.setCurrentItem(pagerPosition);

        options = new DisplayImageOptions.Builder()
            .showImageForEmptyUri(R.drawable.ic_launcher)
            .cacheOnDisc()
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            .build();
    }




    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch(v.getId()){

        case R.id.boutton_retour:
            finish();
        break;

        }
    }
}

我希望它出现在单击时的对话框中。

4

3 回答 3

0

您希望整个活动加载到一个对话框中。有几种方法。如果活动只是一个 ViewPager,您可以只将对话框的内容设置为该 ViewPager:

    Dialog settingsDialog = new Dialog(this);
    settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    ViewPager mViewPager = new ViewPager(this);
    ImagePagerAdapter adapter = new ImagePagerAdapter(images);
    mViewPager.setAdapter(adapter);
    settingsDialog.setContentView(mViewPager);
    settingsDialog.show();

你甚至可以像正常的意图一样加载整个活动,然后让它看起来像一个对话框,有一个对话框主题或其他。

如果您知道.setContentView可以获取资源 id视图,那么使用什么方法应该是不言而喻的。就像onCreate.

于 2012-12-24T13:28:09.577 回答
0

最简单的方法就是在你的 AndroidManifest 中设置:

 <activity
            android:name=".ImagePopUp"
            android:theme="@android:style/Theme.Dialog" >
        </activity>

如果您需要对话框具有自定义大小,您可以在 ImagePopUp 类的开头使用类似这样的内容:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screen_width = dm.widthPixels;
int screen_height = dm.heightPixels;

getWindow().setLayout((screen_width *0.75), (int) (screen_height - 50));
getWindow().setGravity(Gravity.CENTER);

然后,像往常一样打开类,使用点击意图:

Intent intent = new Intent(YourActualClass.this, ImagePopUp.class);
startActivity(intent);
于 2012-12-24T14:17:47.903 回答
0

您可以通过在布局 xml 中使用自定义视图来执行此操作。

 settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.image_pager, null));

布局/imagepaper.xml

<com.your.package.CustomLinearLayout
   xmlns: ... 
   android:layout_width="" ... etc
   />

然后这是对话框布局的“类”

CustomLinearLayout.java

public class CustomLinearLayout extends LinearLayout {

     // Overridden constructors

     // onFinishInflate() etc

}

另一种方法是使用“ DialogFragment

此处显示:http: //blog.blundell-apps.com/tut-generic-fragment-dialog/

您的课程扩展了 DialogFragment:

 public class OneOptionDialogFragment<T> extends DialogFragment {

       // do stuff

 }

并显示对话框(取自上面的 url):

 OneOptionDialogFragment<UserDetails> dialogFragment = OneOptionDialogFragment.newInstance(title, message, buttonText);
    dialogFragment.show(getSupportFragmentManager(), "DeleteFragTag");
于 2012-12-24T14:22:34.890 回答