API Demos 2.2 研读笔记(3)——Custom Dialog

在Android中有一种类似于HTML和CSS将样式和内容分离的机制。我们可以将内容定义在layout的XML中,将样式定义在style的XML中。通过HTML和CSS的实践证明,这种分离更有益于代码的重用和维护。

 

Custom Dialog示例

 

Android官方API Demo中的Custom Dialog就是一个简单的示例。

 

首先,将样式(style)定义在res/values/styles.xml中。

<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog"> 
    <item name="android:windowBackground">@drawable/filled_box</item> 
</style>

其次,将内容(layout)定义在res/layout/custom_dialog_activity.xml中。

<?xml version="1.0" encoding="utf-8"?> 
<!-- This screen consists of a single text field that displays some text. --> 
<TextView xmlns:android=http://schemas.android.com/apk/res/android
 android:id="@+id/text" android:layout_width="match_parent" 
android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal" 
android:text="@string/custom_dialog_activity_text"/>

第三,将样式应用到内容上,见AndroidManifest.xml。 将Activity的theme属性配置成想要的style。

<activity android:name=".app.CustomDialogActivity" 
android:label="@string/activity_custom_dialog" 
android:theme="@style/Theme.CustomDialog"> 
    <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
        <category android:name="android.intent.category.SAMPLE_CODE" /> 
    </intent-filter> 
</activity>

 

在官方的示例中,一个style被应用在一个activity上了,其实一个style还可以被应用在view或者整个application上。如果将一个style应用在activity或者application上,那么这个style就被称作是theme。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值