Android SDK开发范例大全 --3.6样式化的定型对象
2015-12-01 16:26
489 查看
一旦文字一多,总是要有一个个指定文字的大小、颜色也不是办法,那么有没有类似CSS样式的办法,用来指定颜色、大小呢?在Android程序开发过程中,也同样可以通过样式(Style)的方式,初始化TextView的文本颜色、大小。当然这个范例只是抛砖引玉,在Layout的任何对象都可以用样式化的方式来改其外观。
而以下的范例中,将创建两个TextView对象作为对比,使其呈现两种不同的样式作为练习,而Style的写法与先前介绍的颜色常数(color.xml)相同。同样是定义在res/values下面,但其定义的方式则有所差异。
src/com.helloworld/HelloWorldActivity.java
package com.helloworld;
import android.app.Activity;
import android.os.Bundle;
public class HelloworldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/white"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
style="@style/DavidStyleText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
style="@style/DavidStyleText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hidi" />
</LinearLayout>
res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DavidStyleText1">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#EC9237</item>
</style>
<style name="DavidStyleText2">
<item name="android:textSize">14sp</item>
<item name="android:textColor">#FF7F7C</item>
<item name="android:fromAlpha">0.0</item>
<item name="android:toAlpha">0.0</item>
</style>
</resources>
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">使用Drawable背景色的戴维文本</string>
<string name="hidi">使用graphics.Color设置文本颜色</string>
<string name="app_name">Helloworld</string>
<drawable name="white">#FFFFFF</drawable>
</resources>
而以下的范例中,将创建两个TextView对象作为对比,使其呈现两种不同的样式作为练习,而Style的写法与先前介绍的颜色常数(color.xml)相同。同样是定义在res/values下面,但其定义的方式则有所差异。
src/com.helloworld/HelloWorldActivity.java
package com.helloworld;
import android.app.Activity;
import android.os.Bundle;
public class HelloworldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/white"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
style="@style/DavidStyleText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
style="@style/DavidStyleText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hidi" />
</LinearLayout>
res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DavidStyleText1">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#EC9237</item>
</style>
<style name="DavidStyleText2">
<item name="android:textSize">14sp</item>
<item name="android:textColor">#FF7F7C</item>
<item name="android:fromAlpha">0.0</item>
<item name="android:toAlpha">0.0</item>
</style>
</resources>
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">使用Drawable背景色的戴维文本</string>
<string name="hidi">使用graphics.Color设置文本颜色</string>
<string name="app_name">Helloworld</string>
<drawable name="white">#FFFFFF</drawable>
</resources>
相关文章推荐
- Android 详解实现TextView加载带图片标签的Html并按比例缩放
- android点击事件,第一次无效,第二次才响应的问题
- Android设置Progress的颜色
- android 控制启动图标显示
- Android点击事件的四种实现 + 多击事件
- android 设置线程优先级 两种方式
- Android Drawable 那些不为人知的高效用法
- android获取系统环境变量
- Android权限参考
- Android软件开发之界面五大布局
- Android开发中实现用户注册和登陆的代码实例分享
- android局域网通信(三)
- android 获得屏幕宽度和高度
- Android 基础代码实例:音频 均衡器 示波器 声场 等
- 学Android---DatePicker&TimePicker
- Android应用开发的一般文件组织结构讲解
- android开发各种相关干货
- 在android studio中配置百度地图
- android 自定义view 流式布局
- android基础代码实例:录制视频