【问题标题】:Android DialogFragment No_Title feature affects layoutAndroid DialogFragment No_Title 功能影响布局
【发布时间】:2016-01-22 00:11:55
【问题描述】:

我有一个显示 ListView 的 android DialogFragment,每行包含一个标题和图像。一切正常,直到我使用 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 内的按钮...

<?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;
}

ListView 行的 XML

<?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;
}

【问题讨论】:

  • 我通常将 requestFeature 指令放在 onResume 中。在 onCreateView 中它似乎不起作用(即使我从未遇到过您的问题)。如果您尝试这样做会发生什么?
  • @thetonrifles 谢谢你的想法——我试过了,但它抛出了一个运行时异常“在添加内容之前必须调用requestFeature()”——因为onCreateView(或者即使我切换到使用onCreate)总是得到在 onResume 之前调用,我不确定如何解决这个问题 - 必须在到达 onResume 之前添加内容,这意味着我不能在 onResume 中调用 requestFeature()。你如何让 requestFeature() 在 onResume 中工作?不管怎样,我不确定这就是问题所在——当我调用它时,它会起作用——标题消失了。它只会破坏片段的其余部分......
  • 对不起我的错误。我对背景和宽度/高度定义感到困惑。尝试在 onCreateDialog 方法中添加 requestFeature。
  • @thetonrifles 也不走运
  • 嗯好的...我正在尝试实现您的相同对话框来检查行为。我有一些问题。在适配器中,我看到您在 getCount 中使用 mResources,在 getItem 中使用 pictureNames。这些数组之间有什么区别?我还看到您正在使用 AsyncTask 来加载要显示的图片。可以分享一下它的实现吗?

标签: android listview android-fragments android-dialogfragment


【解决方案1】:

试试这个...而不是使用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);
}

【讨论】:

    【解决方案2】:

    您可以在此处找到对话框的完整代码。基本上我删除了 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;
            }
    
        }
    
    }
    

    【讨论】:

    • 谢谢,但这对我不起作用,我有和以前一样的问题,当标题被删除时,格式变成垃圾。不过@wasterley 的回答解决了这个问题。
    猜你喜欢
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多