編輯:初級開發
下面看看第二種方式:在Activity類中進行設置
1、先將main.XML改成如下,即去掉android:textColor="@color/red":
<?xml version="1.0" encoding="utf-8"?> <LinearLayout XMLns:android="http://schemas.android.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);
}
}
第一步:獲得文本控件TextVIEw,取名為tv
第二步:通過TextVIEw的setTextColor方法進行文本顏色的設置,這裡可以有3種方式進行設置:
第1種:tv.setTextColor(android.graphics.Color.RED);//系統自帶的顏色類
第2種:tv.setTextColor(0xffff00ff);//0xffff00ff是int類型的數據,分組一下0x|ff|ff00ff,0x是代表顏色整數的標記,ff是表示透明度,ff00ff表示顏色,注意:這裡ffff00ff必須是8個的顏色表示,不接受ff00ff這種6個的顏色表示。
第3種:tv.setTextColor(this.getResources().getColor(R.color.red));//通過獲得資源文件進行設置。根據不同的情況R.color.red也可以是R.string.red或者R.drawable.red,當然前提是需要在相應的配置文件裡做相應的配置,如:
<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 {
/** 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));
*/
}
}
注:請切換相應的注釋
通過在Activity類中設置文本顏色,我們可以實現文本顏色的動態化。如果想保持文本顏色靜態不變的話,可以直接通過上一篇中講的通過直接配置即可。
Android系統APK軟件漢化教程-2
第一部分:arsc文件漢化目前市面上最流行的漢化方式,就是漢化這部分主文件。我們開始吧:首先,打開androidResEdit1.3然後把resources.arsc文
關於Android中的sdcard卡
最近正在學習android的相關知識,遇到了很多問題,其中之一就是再往sdcard卡中添加文件時會出現下面類似的問題: &
Android開發教程之界面開發
這篇文章沒有打算有一個很好的邏輯去介紹android的某個方面,全盤大致上就是我接觸、了解android的ui開發後到現在的一些感想以及個人理解吧! &nbs
android ListView詳解
在android開發中ListView是比較常用的組件,它以列表的形式展示具體內容,並且能夠根據數據的長度自適應顯示。抽空把對ListVIEw的使用做了整理,並寫了個小