颜色显示

本文介绍了一个简单的Android应用程序,用于演示如何使用代码设置不同透明度的绿色背景到文本视图组件。通过实例展示了六位和八位颜色代码的区别,并在XML布局文件中也设置了相应的背景颜色。

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

MainActivity

package com.example.junior;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;

/**
 * Created by ouyangshen on 2017/9/11.
 */
public class ColorActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color);
        // 从布局文件中获取名叫tv_code_six的文本视图
        TextView tv_code_six = findViewById(R.id.tv_code_six);
        // 给文本视图tv_code_six设置背景为透明的绿色,透明就是看不到
        tv_code_six.setBackgroundColor(0x00ff00);
        // 从布局文件中获取名叫tv_code_eight的文本视图
        TextView tv_code_eight = findViewById(R.id.tv_code_eight);
        // 给文本视图tv_code_eight设置背景为不透明的绿色,即正常的绿色
        tv_code_eight.setBackgroundColor(0xff00ff00);
    }
}

layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_xml"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="XML设置六位背景颜色"
        android:textSize="18sp"
        android:background="#00ff00" />

    <TextView
        android:id="@+id/tv_code_six"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="代码设置六位背景颜色"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv_code_eight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="代码设置八位背景颜色"
        android:textSize="18sp" />

</LinearLayout>

result

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值