Android 自定义对话框的正确姿势 - 官方推荐

Android 自定义对话框的正确姿势 - 官方推荐,第1张

概述前言再写这篇文章之前,已经写了一篇自定义Dialog对话框了。和网上给的大多数解决办法一样,都是继承自Dialog类。但是我感觉这样好麻烦不是我想要的,于是就自己打开了官网Dialog章节看到了官网给的解决办法,眼睛一亮,这就是我想要的。话不多说开始吧看见这段话需要把我这几个 前言

再写这篇文章之前,已经写了一篇自定义Dialog对话框了。和网上给的大多数解决办法一样,都是继承自Dialog类。但是我感觉这样好麻烦不是我想要的,于是就自己打开了官网 Dialog章节看到了官网给的解决办法,眼睛一亮,这就是我想要的。话不多说开始吧

看见这段话需要把我这几个意思:

可以用 Activity@H_301_20@ 实现对话框的形式。而不是使用Dialog API.

需要创建一个Activity@H_301_20@(不是AppCompatActivity@H_301_20@)

AndroIDManifast.xml@H_301_20@ 文件 对应Activity@H_301_20@元素上面添加主题设置 theme.Holo.Dialog@H_301_20@:

<activity androID:theme="@androID:style/theme.Holo.Dialog" >@H_301_20@

就这么简单。Activity就在屏幕一对话框的形式展现了。

添加Activity
import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.WindowManager;public class DialogActivity extends Activity {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_dialog);        getwindow().addFlags(WindowManager.LayoutParams.FLAG_FulLSCREEN);    }}@H_301_20@

对应xml配置

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    tools:context=".DialogActivity">    <linearLayout        androID:ID="@+ID/contentPanel"        androID:layout_alignParenttop="true"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:background="@drawable/dialog_activity_Title_bg"        androID:orIEntation="vertical"        androID:layout_centerHorizontal="true"        androID:layout_margintop="50dp"        androID:padding="18dp">        <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="@string/jianjIETxt"            androID:textcolor="#333a40"            androID:layout_marginBottom="15dp"            androID:textSize="25sp" />        <ScrollVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="match_parent">            <TextVIEw                androID:ID="@+ID/descTV"                androID:layout_wIDth="match_parent"                androID:layout_height="match_parent"                androID:text="@string/jainjIE"                androID:textcolor="@color/activityDialogTextcolor"                androID:textSize="18sp" />        </ScrollVIEw>    </linearLayout>    <Imagebutton        androID:layout_marginBottom="50dp"        androID:layout_alignParentBottom="true"        androID:layout_centerHorizontal="true"        androID:src="@drawable/ic_close_black_24dp"        androID:background="@drawable/dialog_activity_close_btn"        androID:layout_wIDth="50dp"        androID:layout_height="50dp" /></relativeLayout>@H_301_20@

简单设置了样式所以有还有几个样式文件

Activity Dialog界面美化

dialog_activity_Title_bg.xml

<?xml version="1.0" enCoding="utf-8"?><shape xmlns:androID="http://schemas.androID.com/apk/res/androID">    <solID androID:color="@color/white" />    <corners androID:radius="30dp" /></shape>@H_301_20@

ic_close_black_24dp.xml

button的 close 图片如下:

<vector androID:height="24dp" androID:tint="#FFFFFF"    androID:vIEwportHeight="24.0" androID:vIEwportWIDth="24.0"    androID:wIDth="24dp" xmlns:androID="http://schemas.androID.com/apk/res/androID">    <path androID:fillcolor="#FF000000" androID:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/></vector>@H_301_20@

还有对应button样式资源文件

dialog_activity_close_btn.xml

<?xml version="1.0" enCoding="utf-8"?><@R_810_3419@ xmlns:androID="http://schemas.androID.com/apk/res/androID">    <item>        <shape>            <corners androID:radius="100dp"/>            <solID androID:color="#80000000" />        </shape>    </item></@R_810_3419@>@H_301_20@

字符串资源:

<resources>    <string name="app_name">My Application22</string>    <string name="jainjIE">简介 从地球走出的周东皇,抬手间可以会面宇宙间任何一颗星辰,神念一扫可以笼罩以广阔的星宇,万劫不灭的元神更是可以脱离力肉身遨游真个宇宙!一个意外让他回到了千年之前从地球走出的周东皇,抬手间可以会面宇宙间任何一颗星辰,神念一扫可以笼罩以广阔的星宇,万劫不灭的元神更是可以脱离力肉身遨游真个宇宙!一个意外让他回到了千年之前...神念一扫可以笼罩以广阔的星宇,万劫不灭的元神更是可以脱离力肉身遨游真个宇宙!一个意外让他回到了千年之前</string>    <string name="jianjIETxt">简介</string></resources>@H_301_20@

color.xml

<?xml version="1.0" enCoding="utf-8"?><resources>    <color name="activityDialogTextcolor">#5B5F61</color></resources>@H_301_20@
自定义Style
<style name="activityDialog" parent="@androID:style/theme.Holo.Dialog">    <item name="androID:windowBackground">@androID:color/transparent</item>    <item name="androID:windowNoTitle">true</item>    <item name="androID:backgroundDimEnabled">true</item>    <!--触摸背景遮罩层是否关闭,默认true-->    <item name="androID:windowCloSEOntouchOutsIDe">false</item></style>@H_301_20@

之所以要自定义是因为,activity界面外面包裹了一层Dialog框,很丑。这个样式文件很简单,大多数Dialog都是如

AndriodManifast
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"    package="com.example.myapplication22">    <application        androID:allowBackup="true"        androID:icon="@mipmap/ic_launcher"        androID:label="@string/app_name"        androID:roundIcon="@mipmap/ic_launcher_round"        androID:supportsRtl="true"        androID:theme="@style/Apptheme">        <activity            androID:name=".DialogActivity"            androID:theme="@style/activityDialog"></activity>        <activity androID:name=".MainActivity">            <intent-filter>                <action androID:name="androID.intent.action.MAIN" />                <category androID:name="androID.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>@H_301_20@

重点如下:

<activity    androID:name=".DialogActivity"    androID:theme="@style/activityDialog"></activity>@H_301_20@

运行如下:

这个是微信阅读app,点击小说描述更多d出来的。做个大概

总结

以上是内存溢出为你收集整理的Android 自定义对话框的正确姿势 - 官方推荐全部内容,希望文章能够帮你解决Android 自定义对话框的正确姿势 - 官方推荐所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1117202.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)