android 各种控件颜色值的设置(使用Drawable,Color)
2014-05-10 17:57
579 查看
在Android中,如果需要改变控件默认的颜色,包括值的颜色,需要预先在strings.xml中设置,类似字符串,可以反复调用。Android中颜色可以使用drawable或是color来定义。
本例中strings.xml内容:
上面定义了几个颜色值,下面是在布局文件中的调用,main.xml内容:
在Java程序中使用:
本例中strings.xml内容:
<a href="http://www.pocketdigi.com/20110509/266.html" rel="bookmark" style="font-size: 14px; color: rgb(51, 68, 34); text-decoration: none; "><?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, Main!</string> <string name="app_name">Color</string> <drawable name="red">#ff0000</drawable> <color name="gray">#999999</color> <color name="blue">#0000ff</color> <color name="background">#ffffff</color> </resources></a>
上面定义了几个颜色值,下面是在布局文件中的调用,main.xml内容:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/background" > <TextView android:id="@+id/tv1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textColor="@drawable/red" /> <TextView android:id="@+id/tv2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textColor="@color/gray" /> <TextView android:id="@+id/tv3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
在Java程序中使用:
package com.pocketdigi.color; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.widget.TextView; public class Main extends Activity { /** Called when the activity is first created. */ TextView tv1,tv2,tv3; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv1=(TextView)findViewById(R.id.tv1); tv2=(TextView)findViewById(R.id.tv2); tv3=(TextView)findViewById(R.id.tv3); tv3.setTextColor(Color.BLUE);//直接使用android.graphics.Color的静态变量 tv2.setTextColor(this.getResources().getColor(R.color.blue));//使用预先设置的颜色值 } }
相关文章推荐
- android 各种控件颜色值的设置(使用Drawable,Color)
- android 各种控件颜色值的设置(使用Drawable,Color)
- android 各种控件颜色值的设置(使用Drawable,Color)
- android 各种控件颜色值的设置(使用Drawable,Color)
- Android使用代码为textview设置drawableLeft或drawableRight
- android中,如果使用imageButton可以在drawable 中设置一个selector,但是imageView设置不起作用
- Android 中使用xml定义各种Drawable
- android中ColorStateList及StateListDrawable设置Selector
- android中使用java代码设置控件获得焦点
- Android 中控件(如Button)设置drawableLeft方法
- Android 5.0系统 style 默认窗口控件颜色值设置
- Android中各种drawable的使用
- Android中View绘制各种状态的背景图片原理深入分析以及StateListDrawable使用
- android Drawable各种类型使用
- Android中各种drawable的使用
- Android使用程序代码设置控件的margin属性
- Android控件设置——动态View的drawableLeft/ drawableRight/drawableTop/drawableBottom
- Android的Animation之LayoutAnimation使用方法(控件设置动画)
- android的Spinner控件的自定义样式设置以及ArrayAdapter的使用
- Android在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom。