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