android控件的背景色设置

这篇博客介绍了Android中设置控件背景色的常用方法,包括代码实现和效果图展示。作者分享了具体的操作步骤,并邀请读者指出可能存在的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  大家好,首次发表博客,在这儿简单地说下,关于控件设置背景色的常用方法!

  这是代码部分:

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>

  效果图:



  好了!该有的代码和布局资源,已在上面了!只需参照写下,便是上图的效果!常用的设置方法,就是这些!如果有错误的地方,还望指出予以改正!

  最后,谢谢各位的到访!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值