Android开发:使用DialogFragment实现dialog自定义布局

摘要:
使用DialogFragment实现dialog的自定义布局最大的好处是可以更好控制dialog的生命周期。使用DialogFragment:TestFragmentdialog=newTestFragment();dialog.show;show的第一个参数是FragmentManager,第二个参数是给这个dialog设置一个Tag。

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


免责声明:文章转载自《Android开发:使用DialogFragment实现dialog自定义布局》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇程序员修炼之道 从小工到专家读书笔记一个被慕课网拿去做Java就业班终极项目的开源商城项目,推荐给大家下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

下载android的linux内核的方法

1、安装git android的linux内核可以从http://android.git.kernel.org/下载,但下载需要使用git,windows版的git可以从http://code.google.com/p/msysgit/下载,有完全安装版和便携版(portable)两个版本可供选择,不常用git的话选portable版就可以了。 下载por...

《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:离线矢量数据下载

1、前言 1.1、环境准备: ArcGIS for Desktop 10.4.1(10.2.1以上版本即可) ArcGIS for Server 10.4.1 (10.2.1以上版本即可) PostgreSQL、Microsoft SQL Server、或 Oracle 设置企业级地理数据库。 1.2、发布具有同步能力的FeatureService服...

(转)Android之RemoteViews

RemoteViews中的setxxx方法 比如setCharSequence(int viewId, String methodName, CharSequence value); views.setString(R.id.textview01, "setText", battery + "%"); 其中views是RomoteViews的实例, 第一个...

【转】Android APK的数字签名的作用和意义

1. 什么是数字签名? 数字签名就是为你的程序打上一种标记,来作为你自己的标识,当别人看到签名的时候会知道它是与你相关的     2. 为什么要数字签名? 最简单直接的回答: 系统要求的。  Android系统要求每一个Android应用程序必须要经过数字签名才能够安装到系统中,也就是说如果一个Android应用程序没有经过数字签名,是没有...

android自定义控件及自定义组合控件

一、构建自定义控件 构建自定义组件 Android中,你的应用程序程序与View类组件有着一种固定的联系,例如按钮(Button)、文本框(TextView),可编辑文本框(EditText),列表框(ListView),复选框(CheckBox),单选框(RadioButton),滚动条(Gallery),微调器(Spinner), 等等,还有一些比较先...

Android开发模拟器启动失败的解决方法

1. 错误提示信息  错误大概意思:连接到adb(android debug bridge)出现了一个严重的错误,你必须重启adb和Eclipse工具,请确保在位置……adb工具被执行了。 adb:是sdk提供的工具,在android-sdk-windowsplatform-tools目录下,通过adb可以连接到android手机或模拟器。像91手机...