【问题标题】:Cannot Set title of Dialog Using Dialog Fragment无法使用对话框片段设置对话框的标题
【发布时间】:2017-04-19 20:22:19
【问题描述】:

好的,所以基本上我有列表项(按钮)的自定义 ListViewonClick 我想打开一个对话框,该对话框基本上应该显示另一个 ListView。我可以查看两个列表就好了,但问题是我的对话框列表视图没有在对话框中显示列表。它只是上一个列表之上的一个透明列表。但是如果尝试设置对话框标题getDialog().setTitle("Apps List"); 我得到上面提到的错误,这意味着我在第一名没有对话框但是为什么?。

public class ProfileDialog extends android.support.v4.app.DialogFragment {
     .........
       @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            ArrayList<String> listItemTitle = new ArrayList<String>();
            listItemTitle.add("Hello");
            listItemTitle.add("Hello1");
            listItemTitle.add("Hello2");
            listItemTitle.add("Hello3");
            ArrayList<String> listItemType = new ArrayList<String>();
            listItemType.add("Hello");
            listItemType.add("Hello123");
            listItemType.add("1234");
            listItemType.add("Hello6666");
            ArrayList<Integer> listItemId = new ArrayList<Integer>();
            listItemId.add(1);
            listItemId.add(2);
            listItemId.add(3);
            listItemId.add(4);
            ArrayList<Profile> ProfileArrayList = new ArrayList<>();

            for (int i = 0; i < listItemTitle.size(); i++) {
                Profile Profile = new Profile(listItemTitle.get(i), listItemType.get(i), listItemId.get(i));
                ProfileArrayList.add(Profile);

            }

            View   profileView = inflater.inflate(R.layout.fragment_profile_dialog, container, false);
            ListView   profileListView = (ListView) profileView.findViewById(R.id.lstvSelectedProfile);
            getDialog().setTitle("Apps List");
    //        ListView   profileListView = (ListView) LayoutInflater.from(context).inflate(R.layout.fragment_profile_dialog, null);
            if(profileListView.getParent()!=null) {
                ((ViewGroup) profileListView.getParent()).removeView(profileListView);
            }
                SelectedProfileListAdapter adapter = new SelectedProfileListAdapter(getActivity().getApplicationContext(), ProfileArrayList);
                profileListView.setAdapter(adapter);


            return profileListView;
        }

这是我填充第二个列表的适配器。

public class SelectedProfileListAdapter extends BaseAdapter{
    private LayoutInflater layoutInflater;
    private List<Profile> selectedProfileList;

    public SelectedProfileListAdapter(Context context, List<Profile> selectedProfileList) {
        layoutInflater =(LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        this.selectedProfileList = selectedProfileList;
//        this.context = context;
    }

...........
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ProfilesListAdapter.ViewHolder ProfileListViewHolder;
        if (convertView == null) {
            ProfileListViewHolder = new ProfilesListAdapter.ViewHolder();
            convertView = layoutInflater.inflate(R.layout.adapter_profiles_list, parent, false);
            ProfileListViewHolder.title = (TextView) convertView.findViewById(R.id.title);
            ProfileListViewHolder.textView3 = (TextView) convertView.findViewById(R.id.textView3);
            ProfileListViewHolder.imageButtonMore = (ImageButton) convertView.findViewById(R.id.imgButtonMore);
            convertView.setTag(ProfileListViewHolder);
        } else {
            ProfileListViewHolder = (ProfilesListAdapter.ViewHolder) convertView.getTag();
        }
        ProfileListViewHolder.textView3.setText(selectedProfileList.get(position).getTitle());
        ProfileListViewHolder.title.setText(selectedProfileList.get(position).getType());
        ProfileListViewHolder.imageButtonMore = (ImageButton) convertView.findViewById(R.id.imgButtonMore);
        ProfileListViewHolder.imageButtonMore.setTag(selectedProfileList.get(position).getId());


        return convertView;
    }

}

【问题讨论】:

  • 我认为您在 DialogFragment onCreateView() 中返回了错误的视图。您重新发送了“profileListView”,它只是列表。尝试将其更改为“profileView”。希望有所帮助!

标签: android listview android-fragments android-dialogfragment


【解决方案1】:

您可以覆盖onCreateDialog() 方法并在那里添加您的标题...

  @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.setTitle("Apps List");
    return dialog;
}

编辑

在您的 res/styles.xml 中:添加自定义对话框样式

<style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="android:windowNoTitle">false</item>
</style>

现在使用:在你的父片段中应用它

ProfileDialog fragment = new ProfileDialog();
fragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);

【讨论】:

  • 无效。还是一样的
  • 感谢您的回答,这对我有点帮助,但问题是我的对话框看起来不像对话框,它加载在对话框中,但它的背景是透明的。所以我不确定,这里有什么问题。
  • @rafsanahmad007 我有一个类似的用例,但我在 DialogFragment 的 Dialog onCreateDialog() 中添加了一个 TimePickerDialog: TimePickerDialog dialog = new TimePickerDialog(getActivity(), this, hour, minute, false); dialog.setTitle("选择时间");返回对话框; setTitle() 不起作用。关于如何解决的任何想法或想法?
猜你喜欢
  • 2017-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多