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" />



  • 相关阅读:
    CentOS 8上安装Docker
    Missing value auth-url required for auth plugin password
    报错initscripts conflicts with redhat-release-server-7.0-1.el7.x86_64
    Linux RHEL7(CentOS7源) 安装 Nginx
    使用xshell远程连接到linux
    RHEL7更换yum源
    Python使用微信接入图灵机器人
    解决pycharm安装python库报错问题
    python自动化
    鼠标点击效果代码
  • 原文地址:https://www.cnblogs.com/tootwo2/p/6298359.html
Copyright © 2011-2022 走看看