如何将动作栏添加到DialogFragment? [英] How to add action bar to a DialogFragment?

查看:68
本文介绍了如何将动作栏添加到DialogFragment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将操作栏添加到DialogFragment?我找到了一种将活动设置为所需样式的方法,然后将其显示为对话框,如以下链接所示

How to add action bar to a DialogFragment? I found a way to style an activity to the way we require and then display it as a dialog as in the following link ActionBar in a DialogFragment I need to set an action bar on a proper DialogFragment. Is it possible?

推荐答案

作为在上方执行ActionBar操作DialogFragment 指示:即使您可以设置DialogFragment的主题,也无法将ActionBar附加到DialogFragment,但它不会注册为ActionBar,但Dialog.getActionBar()始终返回null.

As Up ActionBar action on DialogFragment indicates: There is no way to attach an ActionBar to the DialogFragment even though you can set the theme of the DialogFragment it will not register as a ActionBar to it, Dialog.getActionBar() will always return null.

但是总有一些情况是我真的想使用DialogFragment(其中包含类似ActionBar的样式)而不是Activity.只需在DialogFragment的布局中添加一个外观类似于ActionBar的布局

But there's always the cases that I really want to use DialogFragment(which contain ActionBar like style) instead of Activity. Just add a Layout that will look like an ActionBar into the DialogFragment's Layout

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

the following is the steps: 1) DialogFragment layout: 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

2) Implement a ActionBar like layout: 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复制的

Note: the @drawable/ic_menu_back is copied from sdk\platforms\android-21\data\res\drawable-hdpi

3)更新DialogFragment代码

3) Update the DialogFragment code

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

希望它能对您有所帮助!

Wish it can help!

这篇关于如何将动作栏添加到DialogFragment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆