大家好,首次发表博客,在这儿简单地说下,关于控件设置背景色的常用方法!
这是代码部分:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
public class ColorPage extends Activity {
LinearLayout ly0;
LinearLayout ly1;
LinearLayout ly2;
LinearLayout ly3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_page);
ly0 = (LinearLayout) findViewById(R.id.ly0);
ly1 = (LinearLayout) findViewById(R.id.ly1);
ly2 = (LinearLayout) findViewById(R.id.ly2);
ly3 = (LinearLayout) findViewById(R.id.ly3);
//格式:#+透明度+颜色值
//透明度:0-100 默认是不透明,即100
//颜色值:000000-ffffff 即黑色到白色
//颜色值可大小写
ly0.setBackgroundColor(Color.parseColor("#6087CEfa"));
//格式:#+透明度+R值+G值+B值
//透明度:0-100 默认是不透明,即100
// R,G,B值 设置范围:0-255
ly1.setBackgroundColor(Color.argb(99, 0, 255, 120));
//格式:#R值+G值+B值 不带透明度
// ly1.setBackgroundColor(Color.rgb(0, 255, 120));
//直接读取Color.Xml的颜色
ly2.setBackgroundColor(R.color.xml_color0);
//通过资源获取,间接读取Color.Xml的颜色
ly3.setBackgroundColor(getResources().getColor(R.color.xml_color1));
}
}
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> 布局文件:</span>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/color_txt"
android:layout_margin="10dp"
android:textSize="20sp" />
<!--该LinearLayout里的TextView,字体颜色通过参数设置的-->
<LinearLayout
android:id="@+id/ly0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Color.parseColor() "
android:textColor="#80000000"
android:textSize="20sp" />
</LinearLayout>
<!--该LinearLayout里的TextView,字体颜色是读取Color.xml中的颜色设置的-->
<LinearLayout
android:id="@+id/ly1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Color.argb()"
android:textColor="@color/xml_txt"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ly2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="直接读取Color.Xml"
android:textColor="#80000000"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ly3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="间接读取Color.Xml"
android:textColor="#80000000"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> 涉及到Color.xml的颜色:</span>
<!--直接获取的颜色-->
<color name="xml_color0">#87CEfa</color>
<!--间接获取的颜色-->
<color name="xml_color1">#ffff00</color>
<!--颜色值为(000000) 透明度为80-->
<color name="xml_txt">#80000000</color>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> 请将上面涉及的颜色,添加到系统里的color.xml文件里。</span>
效果图:
好了!该有的代码和布局资源,已在上面了!只需参照写下,便是上图的效果!常用的设置方法,就是这些!如果有错误的地方,还望指出予以改正!
最后,谢谢各位的到访!