Android TextView字体颜色设置方法小结

2019-12-10 19:04:26于海丽

上面将资源部分分成了3个部分,目的是为了清晰,当然你也可以只建一个xml文件放在res目录下,而且文件名称可以随便命名。

注意两个地方:

1、main.xml的TextView标签中:android:textColor="@color/red"

2、color.xml中:<color name="red">#FF0000</color>

@color指获取资源文件中(所有res目录下的xml文件)的<color>标签

/red指在标签下找其name值为red的内容,此时其值为#FF0000

因此,这里我们还可以这样做:android:textColor="@drawable/red"

@drawable指获取资源文件中<drawable>标签

/red指在标签下找其name值为red的内容

以此类推,相信你也就知道了如果是在strings.xml中该怎么做了。

下面看看第二种方式:在Activity类中进行设置

1、先将main.xml改成如下,即去掉android:textColor="@color/red":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/white"
  >
<TextView
  android:id="@+id/tv01"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  android:autoLink="all"
  />
</LinearLayout>

2、修改Activity的onCreate方法,这里我的Activity是Study03_01,原始代码如下:

package yahaitt.study03_01;
import android.app.Activity;
import android.os.Bundle;
public class Study03_01 extends Activity {    @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }
}