安卓开发设置文本颜色的方式

java yekong 434℃

java设置文字颜色

setTextColor

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.setTextColor(Color.GREEN);
//        设置不透明绿色
        tv_hello.setTextColor(0xff00ff00);
//        设置透明绿色
        tv_hello.setTextColor(0x00ff00);
    }
}

xml设置文本的颜色

<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"
></TextView>
喜欢 (0)