Android开发-Android应用程序设置背景色或者主题

 Android应用程序设置背景色或者主题,可以采用如下的方式:

1. 最常用的,在布局文件(xml文件)中进行设置:

android:background="#ffffff" //设置背景色为白色

2. 通过java代码进行设置:

LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearLayout1);
myLayout.setBackgroundColor(Color.WHITE);

 说明:

setBackgroundColor的功能:设置背景色。

3. 在清单文件AndroidManifest.xml中,通过android:theme属性来设置,既可以使用系统自带的属性值,也可以进行自定义的theme来设置。

<activity 
    android:screenOrientation="portrait" 
    android:name=".ui.MainActivity" 
    android:theme="@android:style/Theme.NoTitleBar">
</activity>

系统常用的自带theme如下:

   @android:style/Theme.Black  //背景黑色-有标题-非全屏
   @android:style/Theme.Black.NoTitleBar //背景黑色-无标题-非全屏
   @android:style/Theme.Black.NoTitleBar.Fullscreen //背景黑色-无标题-全屏显示
   @android:style/Theme.Dialog //对话框显示
   @android:style/Theme.InputMethod
   @android:style/Theme.Light    //背景白色-有标题-非全屏
   @android:style/Theme.Light.NoTitleBar //背景白色-无标题-非全屏
   @android:style/Theme.Light.NoTitleBar.Fullscreen //背景白色-无标题-全屏显示
   @android:style/Theme.Light.Panel
   @android:style/Theme.Light.WallpaperSettings //背景透明
   @android:style/Theme.NoDisplay
   @android:style/Theme.Translucent.NoTitleBar.Fullscreen //半透明、无标题栏、全屏
   @android:style/Theme.Wallpaper.NoTitleBar.Fullscreen

自定义theme:

在style.xml中,进行定义:

<style name="MyTitleBar" parent="android:Theme">
        <item name="android:windowTitleSize">50dip</item>
         <item         name="android:windowTitleBackgroundStyle">@style/MyTitleBackground</item>
        <item name="android:windowTitleStyle">@style/WindowTitle</item>
  </style>
 <!-- 自定义标题栏背景图 -->
  <style name="MyTitleBackground" parent="android:TextAppearance.WindowTitle">
       <item name="android:background">@drawable/bg_topbar</item>
  </style>
  <style name="WindowTitle" parent="android:TextAppearance.WindowTitle">
       <item name="android:singleLine">true</item>
  </style>

说明:

这里的parent是继承于android:Theme,所以在下面的样式里,只能是window开头的样式才起作用,所有样式请参考\sdk\docs\reference\android\R.attr.html,
也可以设置windowTitleBackgroundStyle 为@style/MyTitleBackground,这样就可以在MyTitleBackground里,设置背景图。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值