Theme在Dailog中的使用

注意:自定义的dialog可以如下图一样AlertDialog.Builder的构造器,如果Dialog是自定义的,那么一定要在自定义里面构造器中传入int theme,然后构造器super(context,theme);

 

AlertDdialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_TRADITIONAL);//传统主题

AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);//传统主题

 


 

 

Builder builder= new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_HOLO_DARK);//使用深色背景 主题

 

 

Builder builder = new AlertDialog.Builder(DkalogActivity.this,AlertDialog.THEME_HOLO_LIGHT);//使用浅色背景 主题


 

Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_DARK);//使用默认设备 深色主题

 

 

 

AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_NIGHT);//使用默认设备 浅色主题

 

 

 

【总结】

想要实现AlertDialog的窗口背景色的自定义的话,可以:

1.在res/values/styles.xml中,自定义一个style,设置背景色:

?

1

2

3

<stylename="CustomAlertDialogBackground"parent="@android:style/Theme.Holo.Dialog">

    <itemname="android:background">@android:color/holo_green_light</item>

</style>

注意是

(1)android:background,而不是android:windowBackground

(2)parent,此处是@android:style/Theme.Holo.Dialog,暂时不太清楚,别的,非Dialog的话,是否有效。

2.实现你自己的自定义的AlertDialog类,其中构造函数中,把theme传给super的AlertDialog:

?

1

2

3

4

5

6

publicclassxxxAlertDialog extendsAlertDialog {

 

    protectedxxxAlertDialog(Context context,inttheme) {

        super(context,theme);

        ......

    }

3.创建自定义AlertDialog类时,把对应的之前自己的style传%G9��进去:

?

1

xxxAlertDialog xxxDialog = newxxxAlertDialog(yourContext, R.style.CustomAlertDialogBackground);

如此,即可。

注意:

(1)CustomAlertDialogBackground是我们自定义的那个style。

自定义圆形ProgressBar:
效果图:

圆形ProgressBar的样式主要有以下几个,我们这里以progressBarStyleLarge为例进行样式的修改,其他的类似。

     

<ProgressBar
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"
  style="?android:attr/progressBarStyleLarge"/>  

首先看一下style="?android:attr/progressBarStyleLarge"的源码,在 \frameworks\base\core\res\res\values\styles.xml

<style name="Widget.ProgressBar.Large">
  <item name="android:indeterminateDrawable">@android:drawable/progress_large_white</item>
  <item name="android:minWidth">76dip</item>
  <item name="android:maxWidth">76dip</item>
  <item name="android:minHeight">76dip</item>
  <item name="android:maxHeight">76dip</item>
</style>

看到这一行<item name="android:indeterminateDrawable">@android:drawable/progress_large_white</item>

它的源码,在 \frameworks\base\core\res\res\drawable\progress_large_white.xml

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/spinner_white_76"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromDegrees="0"
    android:toDegrees="360" />

  看到这一行 android:drawable="@drawable/spinner_white_76" 我们就明白了,原来它在这里放了一张图片,进行旋转。

  接下来我定义自己的ProgressBarStyle:

  首先我们先找一张图片加入我们的项目中(如一开始的效果图片),然后在drawable下新建progress_large.xml文件

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/progress_large"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360" />

  在 \value\style.xml中定义myProgressBarStyleLarge

<style name="myProgressBarStyleLarge" >
  <item name="android:indeterminateDrawable">@drawable/progress_large</item>
  <item name="android:minWidth">76dip</item>
  <item name="android:maxWidth">76dip</item>
  <item name="android:minHeight">76dip</item>
  <item name="android:maxHeight">76dip</item>
</style>

  最后在ProgressBar中使用我们自己定义的style,android:indeterminateDuration="700"指定图片旋转的速度,这样我们就可以根据自己的需要来定义ProgressBar的样式。

<ProgressBar
  style="@style/myProgressBarStyleLarge"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:indeterminateDuration="700" />

自定义theme样式:

 

项目的 res / value 目录下 建立xml文件:mytheme.xml 内容如下

<?xml version="1.0""encoding="utf-8"?>
<resources>
<style name="mytheme" parent="android:Theme.Dialog">
然后在这个基础上实现 android:windowNoTitle,也就是不显示标题栏,加上字体大小,字体颜色……

之后的使用和android自带样式使用方法一致。

 

theme样式使用方法:

 

(1)将样式代码放置在应用的AndroidManifest.xml即可。

格式:android:theme="@android:styne/这里写样式代码 "

    比如:android:theoe="@android:style/Theme.Dialog"  

 

 

或者(2)在onCreate()调用之前被调用setTheme()函数:

         setTheme(R.style.Theme_Dialog);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值