Android DialogFragment No_Title 功能影响布局
Android DialogFragment No_Title feature affects layout
我有一个 android DialogFragment 显示一个 ListView,每个包含标题和图像的行。一切都显示正常,直到我使用 getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
从 DialogFragment 中删除标题
删除标题后,图像将停止显示在 ListView 中,所有显示的都是标题。 FEATURE_NO_TITLE 怎么可能影响 DialogFragment 中 ListView 的显示?从字面上看,没有其他代码行发生变化 - 只有那一行,并且图像停止显示在 ListView 中。
对话框片段 OnCreate
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pick_photo_dialog_fragment, container);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
Button closeButton = (Button) view.findViewById(R.id.closeButton);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
listview = (ListView) view.findViewById(R.id.photosListView);
listadapter = new PhotoListAdapter(getActivity());
listview.setAdapter(listadapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//do stuff
}
});
return view;
}
XML 片段只包含一个 ListView 和一个 LinearLayout 内的 Button...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/dark_gray">
<ListView android:id="@+id/photosListView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:clickable="false"
android:listSelector="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
/>
<Button android:id="@+id/closeButton"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:text="Close"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="@color/white"
android:typeface="sans"
android:background="@color/transparent"/>
</LinearLayout>
ListAdapter相关类
int[] mResources = {R.drawable.basketball, R.drawable.bodyweight, R.drawable.boulder, R.drawable.boxing, R.drawable.cardio,
R.drawable.crossfit, R.drawable.olympic, R.drawable.racquet, R.drawable.run, R.drawable.spin, R.drawable.squash,
R.drawable.swim, R.drawable.volleyball, R.drawable.weight, R.drawable.yoga};
String[] pictureNames = {"basketball", "bodyweight", "boulder", "boxing", "cardio", "crossfit", "olympic", "racquet", "run",
"spin", "squash", "swim", "volleyball", "weight", "yoga"};
public PhotoListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
mContext = context;
}
public int getCount() {
return mResources.length;
}
public Object getItem(int position) {
return pictureNames[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.photo_list_row_view, null);
holder = new ViewHolder();
holder.activityNameTV = (TextView) convertView.findViewById(R.id.activityName);
holder.activityImageIV = (ImageView) convertView.findViewById(R.id.activityImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
task = new CreateClassSetImageTask(position, holder.activityNameTV, holder.activityImageIV);
task.execute();
return convertView;
}
static class ViewHolder {
TextView activityNameTV;
ImageView activityImageIV;
}
XML 对于 ListView 行
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/activityName"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text=""
android:textSize="20dp"
android:textStyle="normal"
android:textColor="#FFFFFF"
android:typeface="sans"
android:layout_gravity="left|center_vertical"
/>
<ImageView android:id="@+id/activityImage"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:scaleType="centerInside"/>
</LinearLayout>
加载图像的异步任务
public class CreateClassSetImageTask extends AsyncTask<Void, Void, Bitmap> {
WeakReference<ImageView> imageViewReference;
WeakReference<TextView> textViewReference;
int pictureNum;
public CreateClassSetImageTask(int pictureNumIn, TextView activityNameTVIn, ImageView activityIVIn) {
pictureNum = pictureNumIn;
imageViewReference = new WeakReference<ImageView>(activityIVIn);
textViewReference = new WeakReference<TextView>(activityNameTVIn);
}
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(Bitmap resultBitmap) {
if (imageViewReference != null && resultBitmap != null && textViewReference != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(resultBitmap);
}
final TextView textView = textViewReference.get();
if (textView != null){
textView.setText(pictureNames[pictureNum]);
}
}
}
@Override
protected Bitmap doInBackground(Void... params) {
return decodeSampledBitmapFromResource(mContext.getResources(), mResources[pictureNum], 100, 100);
}
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
在这里您可以找到对话框的完整代码。基本上我删除了 AsyncTask(如果您需要例如从网络下载图片,您最终可以恢复它)并覆盖方法 onCreateDialog
而不是 onCreateView
。
public class PhotoDialog extends DialogFragment {
public static PhotoDialog newInstance() {
return new PhotoDialog();
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View v = getActivity().getLayoutInflater().inflate(R.layout.pick_photo_dialog_fragment, null);
Button closeButton = (Button) v.findViewById(R.id.closeButton);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
ListView listView = (ListView) v.findViewById(R.id.photosListView);
ListAdapter listAdapter = new PhotoListAdapter(getActivity());
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//do stuff
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// REMOVE setTitle in case you don't want dialog title to be shown
//builder.setTitle("Dialog Title");
builder.setView(v);
return builder.create();
}
private static class PhotoListAdapter extends BaseAdapter {
private static ListItem[] _ITEMS = {
new ListItem("basketball", R.drawable.basketball),
new ListItem("bodyweight", R.drawable.bodyweight),
new ListItem("boulder", R.drawable.boulder),
new ListItem("boxing", R.drawable.boxing),
new ListItem("cardio", R.drawable.cardio),
new ListItem("crossfit", R.drawable.crossfit),
new ListItem("olympic", R.drawable.olympic),
new ListItem("racquet", R.drawable.racquet),
new ListItem("run", R.drawable.run),
new ListItem("spin", R.drawable.spin),
new ListItem("squash", R.drawable.squash),
new ListItem("swim", R.drawable.swim),
new ListItem("volleyball", R.drawable.volleyball),
new ListItem("weight", R.drawable.weight),
new ListItem("yoga", R.drawable.yoga)
};
static class ViewHolder {
TextView activityNameTV;
ImageView activityImageIV;
}
static class ListItem {
String title;
int icon;
public ListItem(String title, int icon) {
this.title = title;
this.icon = icon;
}
}
private LayoutInflater mInflater;
public PhotoListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return _ITEMS.length;
}
public Object getItem(int position) {
return _ITEMS[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.photo_list_row_view, null);
holder = new ViewHolder();
holder.activityNameTV = (TextView) convertView.findViewById(R.id.activityName);
holder.activityImageIV = (ImageView) convertView.findViewById(R.id.activityImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ListItem item = (ListItem) getItem(position);
holder.activityNameTV.setText(item.title);
holder.activityImageIV.setImageResource(item.icon);
return convertView;
}
}
}
试试这个...而不是使用 getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
在 Dialog 的 onCreate 方法中设置 DialogFragment.STYLE_NO_TITLE
如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
style = DialogFragment.STYLE_NO_TITLE; break;
theme = android.R.style.Theme_Holo; break;
setStyle(style, theme);
}
我有一个 android DialogFragment 显示一个 ListView,每个包含标题和图像的行。一切都显示正常,直到我使用 getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
从 DialogFragment 中删除标题删除标题后,图像将停止显示在 ListView 中,所有显示的都是标题。 FEATURE_NO_TITLE 怎么可能影响 DialogFragment 中 ListView 的显示?从字面上看,没有其他代码行发生变化 - 只有那一行,并且图像停止显示在 ListView 中。
对话框片段 OnCreate
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pick_photo_dialog_fragment, container);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
Button closeButton = (Button) view.findViewById(R.id.closeButton);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
listview = (ListView) view.findViewById(R.id.photosListView);
listadapter = new PhotoListAdapter(getActivity());
listview.setAdapter(listadapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//do stuff
}
});
return view;
}
XML 片段只包含一个 ListView 和一个 LinearLayout 内的 Button...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/dark_gray">
<ListView android:id="@+id/photosListView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:clickable="false"
android:listSelector="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
/>
<Button android:id="@+id/closeButton"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:text="Close"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="@color/white"
android:typeface="sans"
android:background="@color/transparent"/>
</LinearLayout>
ListAdapter相关类
int[] mResources = {R.drawable.basketball, R.drawable.bodyweight, R.drawable.boulder, R.drawable.boxing, R.drawable.cardio,
R.drawable.crossfit, R.drawable.olympic, R.drawable.racquet, R.drawable.run, R.drawable.spin, R.drawable.squash,
R.drawable.swim, R.drawable.volleyball, R.drawable.weight, R.drawable.yoga};
String[] pictureNames = {"basketball", "bodyweight", "boulder", "boxing", "cardio", "crossfit", "olympic", "racquet", "run",
"spin", "squash", "swim", "volleyball", "weight", "yoga"};
public PhotoListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
mContext = context;
}
public int getCount() {
return mResources.length;
}
public Object getItem(int position) {
return pictureNames[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.photo_list_row_view, null);
holder = new ViewHolder();
holder.activityNameTV = (TextView) convertView.findViewById(R.id.activityName);
holder.activityImageIV = (ImageView) convertView.findViewById(R.id.activityImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
task = new CreateClassSetImageTask(position, holder.activityNameTV, holder.activityImageIV);
task.execute();
return convertView;
}
static class ViewHolder {
TextView activityNameTV;
ImageView activityImageIV;
}
XML 对于 ListView 行
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/activityName"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text=""
android:textSize="20dp"
android:textStyle="normal"
android:textColor="#FFFFFF"
android:typeface="sans"
android:layout_gravity="left|center_vertical"
/>
<ImageView android:id="@+id/activityImage"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:scaleType="centerInside"/>
</LinearLayout>
加载图像的异步任务
public class CreateClassSetImageTask extends AsyncTask<Void, Void, Bitmap> {
WeakReference<ImageView> imageViewReference;
WeakReference<TextView> textViewReference;
int pictureNum;
public CreateClassSetImageTask(int pictureNumIn, TextView activityNameTVIn, ImageView activityIVIn) {
pictureNum = pictureNumIn;
imageViewReference = new WeakReference<ImageView>(activityIVIn);
textViewReference = new WeakReference<TextView>(activityNameTVIn);
}
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(Bitmap resultBitmap) {
if (imageViewReference != null && resultBitmap != null && textViewReference != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(resultBitmap);
}
final TextView textView = textViewReference.get();
if (textView != null){
textView.setText(pictureNames[pictureNum]);
}
}
}
@Override
protected Bitmap doInBackground(Void... params) {
return decodeSampledBitmapFromResource(mContext.getResources(), mResources[pictureNum], 100, 100);
}
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
在这里您可以找到对话框的完整代码。基本上我删除了 AsyncTask(如果您需要例如从网络下载图片,您最终可以恢复它)并覆盖方法 onCreateDialog
而不是 onCreateView
。
public class PhotoDialog extends DialogFragment {
public static PhotoDialog newInstance() {
return new PhotoDialog();
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View v = getActivity().getLayoutInflater().inflate(R.layout.pick_photo_dialog_fragment, null);
Button closeButton = (Button) v.findViewById(R.id.closeButton);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
ListView listView = (ListView) v.findViewById(R.id.photosListView);
ListAdapter listAdapter = new PhotoListAdapter(getActivity());
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//do stuff
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// REMOVE setTitle in case you don't want dialog title to be shown
//builder.setTitle("Dialog Title");
builder.setView(v);
return builder.create();
}
private static class PhotoListAdapter extends BaseAdapter {
private static ListItem[] _ITEMS = {
new ListItem("basketball", R.drawable.basketball),
new ListItem("bodyweight", R.drawable.bodyweight),
new ListItem("boulder", R.drawable.boulder),
new ListItem("boxing", R.drawable.boxing),
new ListItem("cardio", R.drawable.cardio),
new ListItem("crossfit", R.drawable.crossfit),
new ListItem("olympic", R.drawable.olympic),
new ListItem("racquet", R.drawable.racquet),
new ListItem("run", R.drawable.run),
new ListItem("spin", R.drawable.spin),
new ListItem("squash", R.drawable.squash),
new ListItem("swim", R.drawable.swim),
new ListItem("volleyball", R.drawable.volleyball),
new ListItem("weight", R.drawable.weight),
new ListItem("yoga", R.drawable.yoga)
};
static class ViewHolder {
TextView activityNameTV;
ImageView activityImageIV;
}
static class ListItem {
String title;
int icon;
public ListItem(String title, int icon) {
this.title = title;
this.icon = icon;
}
}
private LayoutInflater mInflater;
public PhotoListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return _ITEMS.length;
}
public Object getItem(int position) {
return _ITEMS[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.photo_list_row_view, null);
holder = new ViewHolder();
holder.activityNameTV = (TextView) convertView.findViewById(R.id.activityName);
holder.activityImageIV = (ImageView) convertView.findViewById(R.id.activityImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ListItem item = (ListItem) getItem(position);
holder.activityNameTV.setText(item.title);
holder.activityImageIV.setImageResource(item.icon);
return convertView;
}
}
}
试试这个...而不是使用 getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
在 Dialog 的 onCreate 方法中设置 DialogFragment.STYLE_NO_TITLE
如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
style = DialogFragment.STYLE_NO_TITLE; break;
theme = android.R.style.Theme_Holo; break;
setStyle(style, theme);
}