在对话框中设置工具栏标题

时间:2015-02-03 19:08:56

标签: android android-layout android-dialog android-toolbar

我创建了一个带有自定义布局的Dialog,允许我在对话框中显示带有自定义对象的ListView。我还在Toolbar添加了android.support.v7.widget.ToolbarDialog),以便为它提供更多实质内容。我想弄清楚如何设置Toolbar的标题,但我遇到了一些问题。通常我会做以下事情:

myToolbar = (Toolbar) findViewById(R.id.my_toolbar);            
setSupportActionBar(myToolbar);

但是,我不能在我创建的类中使用它来构建Dialog,因为setSupportActionBarActionBarActivity方法。我也尝试使用setTitle来指定标题文本,但无济于事。

那么,在不使用任何Toolbar方法的情况下设置ActionBarActivity标题的最佳方法是什么?我的代码如下所示:

Dialog布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/dialog_toolbar"
        android:layout_height="52dp"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />

    <ListView
        android:id="@+id/group_listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingTop="5dp"
        android:cacheColorHint="@android:color/transparent"
        android:choiceMode="multipleChoice"
        android:layout_weight="1"
        android:isScrollContainer="false"
        android:divider="@color/grey_300"
        android:dividerHeight="1dip"
        android:fadingEdge="none">
    </ListView>

    <LinearLayout
        android:id="@+id/btnHolderLL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/dialog_ok_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingRight="1dp"
            android:paddingLeft="1dp"
            android:textSize="17dp"
            android:textColor="#FFFFFF"
            android:textAllCaps="false"
            android:background="@drawable/button_selector"
            android:text="Ok"
            android:clickable="true"/>
    </LinearLayout>

</LinearLayout>

Dialog构建器类

public class ListGroup
{
    public static Dialog CreateGroupDialog(Context context, ArrayList<ListObject> arrayList)
    {
        final Dialog dialog = new Dialog(context, R.style.dialogTheme);
        dialog.setContentView(R.layout.group_dialog);
        Button dialogButton = (Button)dialog.findViewById(R.id.dialog_ok_button);
        dialogButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                dialog.dismiss();
            }
        });

        LayoutInflater li = LayoutInflater.from(context);
        View view = li.inflate(R.layout.group_dialog, null);
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.dialog_toolbar);
        // The following line has no effect...
        toolbar.setTitle("List Group");

        dialog.setCancelable(false);
        ListView listView = (ListView) dialog.findViewById(R.id.group_listview);
        CustomAdapter customAdapter = new CustomAdapter(context, R.layout.list_object, arrayList);
        listView.setAdapter(customAdapter);
        return dialog;
    }
}

作为一个例子,我会像这样创建一个Dialog

ArrayList arrayList = new ArrayList<ListObject>();
ListObject listObject1 = new ListObject("Item #1", new String[]{""}, "", "", "", "blank",
        "blank", "blank", "blank", "blank", "blank", "", 1, false, 0, 0, false, false, null, null);
ListObject listObject2 = new ListObject("Item #2", new String[]{""}, "", "", "", "blank",
        "blank", "blank", "blank", "blank", "blank", "", 1, false, 0, 0, false, false, null, null);
ListObject listObject3 = new ListObject("Item #3", new String[]{""}, "", "", "", "blank",
        "blank", "blank", "blank", "blank", "blank", "", 1, false, 0, 0, false, false, null, null);
arrayList.add(listObject1);
arrayList.add(listObject2);
arrayList.add(listObject3);
Dialog groupDialog = ListGroup.CreateGroupDialog(MainActivity.this, null, arrayList);
groupDialog.show();

这会创建如下所示的Dialog;正如你所看到的,没有标题......

enter image description here

1 个答案:

答案 0 :(得分:1)

使用时

 LayoutInflater li = LayoutInflater.from(context);
 View view = li.inflate(R.layout.group_dialog, null);
 Toolbar toolbar = (Toolbar) view.findViewById(R.id.dialog_toolbar);

您正在对新布局进行充气并引用新的Toolbar,而不是使用您的对话框使用的那个(在dialog.setContentView(R.layout.group_dialog);中夸大了)。与您在对话框中引用Button的方式类似,您应该使用以下方法检索工具栏:

Toolbar toolbar = (Toolbar) dialog.findViewById(R.id.dialog_toolbar);