|
繼《android TextView的字體顏色設(shè)置的多種方法》【http://yahaitt./blog/454439】
下面看看第二種方式:在
Activity類中進行設(shè)置
1、
先將main.xml改成如下,即去掉android:textColor="@color/red":
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas./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>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas./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);
- }
- }
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);
}
}
第一步:獲得文本控件TextView,取名為tv
第二步:通過TextView的setTextColor方法進行文本顏色的設(shè)置,這里可以有3種方式進行設(shè)置:
第1種:tv.setTextColor(android.graphics.Color.RED);//系統(tǒng)自帶的顏色類
第2種:tv.setTextColor(0xffff00ff);//0xffff00ff是int類型的數(shù)據(jù),分組一下
0x|ff|ff00ff,0x是代表顏色整數(shù)的標記,ff是表示透明度,ff00ff表示顏色,注意:這里ffff00ff必須是8個的顏色表示,不接
受ff00ff這種6個的顏色表示。
第3種:tv.setTextColor(this.getResources().getColor(R.color.red));//通過獲得
資源文件進行設(shè)置。根據(jù)不同的情況R.color.red也可以是R.string.red或者R.drawable.red,當然前提是需要在相應(yīng)的配
置文件里做相應(yīng)的配置,如:
<color name="red">#FF0000</color>
<drawable name="red">#FF0000</drawable>
<string name="red">#FF0000</string>
詳細的代碼如下:
- package yahaitt.study03_01;
-
- import android.app.Activity;
- import android.content.res.Resources;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.widget.TextView;
-
- public class Study03_01 extends Activity {
-
- private TextView tv;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- tv = (TextView)this.findViewById(R.id.tv01);
-
-
-
-
-
-
-
-
-
- }
- }
package yahaitt.study03_01;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class Study03_01 extends Activity {
/** Called when the activity is first created. */
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)this.findViewById(R.id.tv01);
// tv.setTextColor(Color.RED);
// tv.setTextColor(0xff000000);
/*
Resources rs = this.getResources();
tv.setTextColor(rs.getColor(R.drawable.red));
*/
}
}
注:請切換相應(yīng)的注釋
通過在Activity類中設(shè)置文本顏色,我們可以實現(xiàn)文本顏色的動態(tài)化。如果想保持文本顏色靜態(tài)不變的話,可以直接通過上一篇中講的通過直接配置
即可。
|