安卓开发设置背景颜色的方式

java yekong 594℃

java设置背景颜色

使用setBackgroundColor或者setBackgroundResource设置背景色

package com.example.chapter03;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class TextViewActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);
        TextView tv_hello = findViewById(R.id.tv_hello);
        tv_hello.setTextSize(30);
        tv_hello.setBackgroundColor(Color.GREEN);
  //      资源文件颜色
        tv_hello.setBackgroundResource(R.color.black);
    }
}

xml设置背景颜色

使用background设置背景色

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <TextView
            android:id="@+id/tv_hello"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name"
            android:textSize="30sp"
            android:textColor="#00ff00"
            android:background="@color/white"
            ></TextView>
</LinearLayout>
喜欢 (0)