Android:设置背景色以及theme(主题)设置(一)

本文介绍了在Android中设置背景色的三种方法:XML布局文件、Java代码和通过AndroidManifest.xml设置主题。同时讲解了如何使用系统自带及自定义主题,包括全局和单个Activity的应用,并给出了在style.xml中定义自定义样式的示例。

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

设置背景色一般有如下三种方法
一、直接在xml文件里设置:

android:background="#ffffff" //RGB16进制表现的方法

二、在java文件里设置:

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

或者

LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearLayout);
myLayout.setBackgroundColor(Color.parseColor("#ffffff"));

三、在AndroidManifest.xml里利用android:theme来设置背景,如下所示:
theme的设置可以设置为系统自带的格式,也可以自定义格式。
A: 系统自带格式

   @android:style/Theme.Black  //背景黑色-有标题-非全屏
   @android:style/Theme.Black.NoTitleBar //背景黑色-无标题-非全屏
   @android:style/Theme.Black.NoTitleBar.Fullscreen //背景黑色-无标题-全屏显示

   @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

注意: 可以在applicaiton里全局设置,也可以在单个Activity里设置。比如:

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

B:也可以自定义
在activity里加入 android:theme=”@style/MyTitleBar” 再在 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里,设置背景图。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值