android-actionbar - 如何向 DialogFragment 添加操作栏?

标签 android-actionbar android-dialogfragment

如何将操作栏添加到DialogFragment?我找到了一种方法,可以按照我们需要的方式设置事件的样式,然后将其显示为对话框,如以下链接 ActionBar in a DialogFragment 所示。我需要在正确的 DialogFragment 上设置操作栏。可能吗?

最佳答案

Up ActionBar action on DialogFragment 表示:无法将 ActionBar 附加到 DialogFragment,即使您可以设置 DialogFragment 的主题,它也不会注册为 ActionBar,Dialog.getActionBar() 将始终返回 null。

但总有一些情况我真的想使用DialogFragment(其中包含类似ActionBar的样式)而不是Activity。只需将一个看起来像 ActionBar 的布局添加到 DialogFragment 的布局中

步骤如下: 1)DialogFragment布局:about_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white" >

<include android:id="@+id/fake_action_bar"
    layout="@layout/fake_action_bar_with_backbotton" />

2) 实现类似 ActionBar 的布局:fake_action_bar_with_backbotton.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fake_action_bar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    app:navigationIcon="@drawable/ic_menu_back"
    android:background="@color/background_material_dark"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

注意:@drawable/ic_menu_back 是从 sdk\platforms\android-21\data\res\drawable-hdpi 复制的

3)更新DialogFragment代码

public class AboutDialogFragment extends DialogFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // use Theme_Holo_Light for full screen
        // use Theme_Holo_Dialog for not full screen
        // first parameter: DialogFragment.STYLE_NO_TITLE   no title
        setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Holo_Light_DarkActionBar);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.about_dialog, container, false);

        // set the listener for Navigation
        Toolbar actionBar = (Toolbar) v.findViewById(R.id.fake_action_bar);
        if (actionBar!=null) {
            final AboutDialogFragment window = this;
            actionBar.setTitle(R.string.about_title);
            actionBar.setNavigationOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    window.dismiss();
                }
            });
        }
        return v;
    }
}

希望对大家有帮助!

关于android-actionbar - 如何向 DialogFragment 添加操作栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22754775/

相关文章:

android - 无法让 Actionbar 图标进行动画处理

android - 如何使用 Android View Pager + ActionBar 选项卡将 View 发送到 Google Analytics?

Android ActionBar 模式 - 多个 Activity 或 fragment

Android - 来自支持库的 fragment 与 4.3 平台不兼容

java - 不显示操作栏溢出

android - 使用 DialogFragment 时出现 IllegalStateException

android - DailogFragment - getArguments/setArguments - 为什么要在包中传递参数?

java - 如何创建新的 "circle"datepicker android 对话框

android - 我应该使用 PopupWindow 或 DialogFragment 来接受输入吗?

android - DialogFragment 上的 ActionBar Action