Android ApiDemos示例解析(107):Views->Controls->1. Light Theme

本文探讨了Android应用中LightTheme与DefaultTheme的实现方式,通过使用R.layout.controls_1的布局文件,展示了如何在不同主题下自定义UI控件,如Button、EditText、CheckBox等,并特别介绍了如何通过RadioGroup和RadioButton实现单选钮功能。同时,通过AndroidManifest.xml为Activity定义不同的风格,实现了浅色和默认主题之间的切换。

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

本例Light Theme 和下例Default Theme ,代码和资源部分几乎一模一样。都使用R.layout.controls_1的 Layout ,Controls1.java 和Controls2.java 差别只在类的名称。

controls_1.xml 定义了下面几种UI控件:Button,EditText,CheckBox,RadioButton,ToggleButton ,Spinner ,TextView 有的前面以有介绍,有的后面会有详细说明。

这里提一下 RadioButton ,RadioButton为单选钮,需要为一组单选钮定义一个组,组内单选钮只能有一个选中,组定义为RadioGroup,如:

<RadioGroup
 android:layout_width=”match_parent”
 android:layout_height=”wrap_content”
 android:orientation=”vertical”>
 
<RadioButton android:id=”@+id/radio1″
 android:layout_width=”wrap_content”
 android:layout_height=”wrap_content”
 android:text=”@string/controls_1_radiobutton_1″ />
 
<RadioButton android:id=”@+id/radio2″
 android:layout_width=”wrap_content”
 android:layout_height=”wrap_content”
 android:text=”@string/controls_1_radiobutton_2″ />
 
</RadioGroup>

此外,CheckBox 可以定义不同的风格,比如本例使用一个“星星”形状:

<CheckBox android:id=”@+id/star”
style=”?android:attr/starStyle”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/controls_1_star” />

其实大部分UI控件都可以自定义风格。

本例和下例一个使用“浅色”风格,一个使用缺省风格,这是通过在AndroidMainifest.xml 为Activity 定义了不同的风格,比如本例:

<activity android:name=”.view.Controls1″
android:label=”Views/Controls/1. Light Theme”
android:theme=”@android:style/Theme.Light”>
<intent-filter>
< action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.SAMPLE_CODE” />
< /intent-filter>
< /activity>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值