是否可以在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;
}
}
}
我想让它出现在点击对话框中。
答案 0 :(得分: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
一样。
答案 1 :(得分: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);
答案 2 :(得分: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
}
并显示对话框(取自上面的网址):
OneOptionDialogFragment<UserDetails> dialogFragment = OneOptionDialogFragment.newInstance(title, message, buttonText);
dialogFragment.show(getSupportFragmentManager(), "DeleteFragTag");