您的位置:首页 > Web前端 > CSS

样式<style>资源

2016-06-24 10:06 381 查看
Android 的样式资源文件也放在/res/values 目录下,样式资源文件的根元素是(resources…/>)元素,该元素内可包含多个(style…/>)子元素,每个(style…/>)元素定义一个样式.

(style…/>) 元素指定如下两个属性:

name: 指定样式的名称.

parent: 指定该样式所继承的父样式.当继承某个父样式时,该样式会获得父样式中定义的全部格式.当前样式也可以覆盖父样式中指定的格式.

package com.test.styleandtheme;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

/**
* 样式 <Style></>的学习
*/
public class StyleDemoActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_style_demo);
}
}


布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.test.styleandtheme.StyleDemoActivity">

<!--指定样式1的格式-->
<EditText
style="@style/style1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/style1"
/>

<!--指定style2的格式-->
<EditText
style="@style/style2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/style2"
/>
</LinearLayout>


样式 资源文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--定义一个样式 指定字体大小和颜色-->
<style name="style1" >
<item name="android:textColor" >#f55</item>
<item name="android:textSize">20sp</item>

</style>

<!--定义一个样式 继承前一个样式-->
<style name="style2" parent="style1">

<item name="android:background">#f70</item>
<item name="android:padding">7dp</item>

<!--覆盖父样式中指定的属性-->
<item name="android:textColor" >#000</item>

</style>
</resources>


字符串资源文件

<resources>
<string name="app_name">样式(style)资源的demo</string>
<string name="style1">样式1的格式</string>
<string name="style2">样式2的格式</string>
</resources>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: