zoukankan      html  css  js  c++  java
  • Android开发:使用DialogFragment实现dialog自定义布局

    使用DialogFragment实现dialog的自定义布局最大的好处是可以更好控制dialog的生命周期。

    TestFragment的代码:
    public class TestFragment extends DialogFragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_test, container, false);
        }
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            Dialog dialog = super.onCreateDialog(savedInstanceState);
    //设置actionbar的隐藏 //dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); return dialog; } }

      

    fragment_test的布局
    <FrameLayout 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"
        tools:context="com.topsports.testapplication.BigImageFragment">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除"/>
    </FrameLayout>

    在TestFragment的onCreateDialog方法中引入自定义布局。

    使用DialogFragment:

    TestFragment dialog=new TestFragment();dialog.show(getSupportFragmentManager(),"dialog");

    show的第一个参数是FragmentManager(这里使用的是android.support.v4.app.DialogFragment,如果使用的是android.app.DialogFragment将getSupportFragmentManager方法替换成getFragmentManager),第二个参数是给这个dialog设置一个Tag。

    PS:如果要实现一个自定义dialog除了使用dialogFragment,还可以用dialog的形式显示activity,可以在AndroidManifest.xml里面设置activity的theme为“@android:style/Theme.Holo.Dialog”。

    <activity android:name=".TestActivity" android:theme="@android:style/Theme.Holo.Dialog" />



  • 相关阅读:
    弹性盒子模型属性之flex-shrink
    Git----基本操作
    Git----简介
    ES6常用语法
    nginx学习
    Shell基础命令(二)
    Linux目录
    Shell基础命令(一)
    CRM之分页
    Django之ModelForm组件
  • 原文地址:https://www.cnblogs.com/tootwo2/p/6298359.html
Copyright © 2011-2022 走看看