Android初级教程九——更改手机窗口画面底色
2011-09-19 01:05
351 查看
① 编写main 布局
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
> <
TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号"
android:layout_x="61px"
android:layout_y="69px"
> </TextView>
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:layout_x="61px"
android:layout_y="158px"
> </TextView>
<EditText
android:id="@+id/name_in"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="114px"
android:layout_y="57px"
> </EditText>
<EditText
android:id="@+id/pwd_in"
android:layout_width="120dip"
android:layout_height="wrap_content"
② 在values 文件夹中定义一个drawable.xml 文件用来存放颜色值
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="darkgray">#938192</color>
<color name="lightgreen">#7cd12e</color>
</resources>
③ 修改main.xml 中的屏幕背景颜色和TextView 的字体颜色
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
xmlns:android="http://schemas.android.com/apk/res/android"
> <
TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号"
android:textColor="@color/darkgray"
android:layout_x="61px"
android:layout_y="69px"
> </TextView>
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textColor="@color/darkgray"
android:layout_x="61px"
android:layout_y="158px"
>
④ 在mainActivity.java 代码中修改TextView 背景颜色
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text=(TextView)findViewById(R.id.name);
//由ID获得资源
Resources myColor=getBaseContext().getResources();
//getBaseContext()获得基础Context
//getResources()获得资源
Drawable color_M=myColor.getDrawable(R.color.lightgreen );
//由资源myColor来获得Drawable R.color.lightgreen是颜色值的ID引用
text.setBackgroundDrawable(color_M);
//设置背景
}
⑤ 结果:略
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
> <
TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号"
android:layout_x="61px"
android:layout_y="69px"
> </TextView>
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:layout_x="61px"
android:layout_y="158px"
> </TextView>
<EditText
android:id="@+id/name_in"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="114px"
android:layout_y="57px"
> </EditText>
<EditText
android:id="@+id/pwd_in"
android:layout_width="120dip"
android:layout_height="wrap_content"
② 在values 文件夹中定义一个drawable.xml 文件用来存放颜色值
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="darkgray">#938192</color>
<color name="lightgreen">#7cd12e</color>
</resources>
③ 修改main.xml 中的屏幕背景颜色和TextView 的字体颜色
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
xmlns:android="http://schemas.android.com/apk/res/android"
> <
TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号"
android:textColor="@color/darkgray"
android:layout_x="61px"
android:layout_y="69px"
> </TextView>
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textColor="@color/darkgray"
android:layout_x="61px"
android:layout_y="158px"
>
④ 在mainActivity.java 代码中修改TextView 背景颜色
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text=(TextView)findViewById(R.id.name);
//由ID获得资源
Resources myColor=getBaseContext().getResources();
//getBaseContext()获得基础Context
//getResources()获得资源
Drawable color_M=myColor.getDrawable(R.color.lightgreen );
//由资源myColor来获得Drawable R.color.lightgreen是颜色值的ID引用
text.setBackgroundDrawable(color_M);
//设置背景
}
⑤ 结果:略
相关文章推荐
- Android基础教程(二)之------更改手机窗口画面底色
- 3.2 MonoForAndroid用户人机界面-更改手机窗口画面底色
- 更改手机窗口画面底色——color.xml定义颜色
- 更改手机窗口画面底色和控件颜色
- 更改手机窗口画面底色
- 更改手机窗口画面底色-----drawable定义颜色常数的方法
- Android SDK 开发范例大全---3.2更改手机窗口画面底色
- 更改手机窗口画面底色
- Android学习笔记(二) —— 更改手机窗口画面底色—color定义颜色常数的方法
- Android初级教程调用手机拍照与摄像功能
- Android初级教程调用手机拍照与摄像功能
- Android初级教程使用服务注册广播接收者监听手机解锁屏变化
- Android初级教程十——手机页面的转换setContentView的应用
- Android初级教程使用服务注册广播接收者监听手机解锁屏变化
- Android初级教程获取手机位置信息GPS与动态获取最佳方式
- Android初级教程_在电脑上共享手机屏幕
- Android初级教程获取手机系统联系人信息
- Android初级教程获取手机位置信息GPS与动态获取最佳方式
- Android初级教程_在电脑上共享手机屏幕