设置theme实现类似于自定义dialog效果

本文介绍如何通过设置Android Activity的主题来实现自定义对话框样式,包括透明背景、无标题及半透明效果等,并提供了具体的XML配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们知道我们要实现自定义dialog可以自己自定义,那么还有一种方法就是让activity变成窗体,那么我们在设置Theme就可以了 
我们来看效果 
这里写图片描述 
点击后弹出一个类似气泡的东西

Android平台定义的主题样式:

android:theme=”@android:style/Theme.Dialog” 将一个Activity显示为对话框模式

•android:theme=”@android:style/Theme.NoTitleBar” 不显示应用程序标题栏 
•android:theme=”@android:style/Theme.NoTitleBar.Fullscreen” 不显示应用程序标题栏,并全屏

•android:theme=”@android:style/Theme.Light” 背景为白色 
•android:theme=”@android:style/Theme.Light.NoTitleBar” 白色背景并无标题栏 
•android:theme=”@android:style/Theme.Light.NoTitleBar.Fullscreen” 白色背景,无标题栏,全屏

•android:theme=”@android:style/Theme.Black” 背景黑色 
•android:theme=”@android:style/Theme.Black.NoTitleBar” 黑色背景并无标题栏 
•android:theme=”@android:style/Theme.Black.NoTitleBar.Fullscreen” 黑色背景,无标题栏,全屏

•android:theme=”@android:style/Theme.Wallpaper” 用系统桌面为应用程序背景 
•android:theme=”@android:style/Theme.Wallpaper.NoTitleBar” 用系统桌面为应用程序背景,且无标题栏 
•android:theme=”@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen” 用系统桌面为应用程序背景,无标题栏,全屏

•android:theme=”@android:style/Translucent” 半透明效果 
•android:theme=”@android:style/Theme.Translucent.NoTitleBar” 半透明并无标题栏 
•android:theme=”@android:style/Theme.Translucent.NoTitleBar.Fullscreen” 半透明效果,无标题栏,全屏 
•android:theme=”@android:style/Theme.Panel” 
我们来看看代码

新建一个activity 在manifest声明 然后设置Theme为自己写的style 我的叫”DialogStyle” 继承于@android:style/Theme.Dialog 
设置属性

<style name="DialogStyle" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        //设置背景为透明
        <item name="android:windowNoTitle">true</item>
      //  设置无标题
        <item name="android:backgroundDimEnabled">true</item>
        //  此属性可以翻译为 是否允许对话框的背景变暗?如果允许背景就变暗了。
    </style>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

弹出的activity的xml

<RelativeLayout 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=".MainActivity" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/bubble"
        android:text="@string/hello_world" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="94dp"
        android:layout_marginLeft="26dp"
        android:orientation="vertical" >
    </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="183dp"
        android:text="我是对话框"
        android:textSize="30sp" />

</RelativeLayout>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

如果要实现自定义dialog也可以这样呦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值