android 設(shè)置控件的顏色字體的方法
1.用代碼設(shè)置控件的顏色:
int b = getResources().getColor(R.drawable.blue);//得到配置文件里的顏色
mButton.setTextColor(b);
2.設(shè)置空間的字體:
方式一:mText.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"));//設(shè)置字體
注意:1.保證文件一定是ttf格式;2.放到assets/fonts目錄下;3.如果找不到相應(yīng)的字體不會(huì)報(bào)錯(cuò),只是在運(yùn)行的時(shí)候顯示不出來
方式二: fontButton.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));//用內(nèi)部支持的方式設(shè)置
package com.oyzz.ch3_6;
import android.app.Activity;
/*必須引用graphics.Color才能使用Color.*的對(duì)象*/
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
/*必須引用 widget.Button才能聲明使用Button對(duì)象*/
import android.widget.Button;
/*必須引用 widget.TextView才能聲明使用TestView對(duì)象*/
import android.widget.TextView;
public class Ch3_6 extends Activity
{
private Button mButton;
private TextView mText;
private int[] mColors;
private int colornum;
private Button fontButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*通過findViewById構(gòu)造器來使用main.xml與string.xml
中button與textView的參數(shù)*/
mButton=(Button) findViewById(R.id.mybutton);
mText= (TextView) findViewById(R.id.mytext);
fontButton=(Button) findViewById(R.id.mybutton1);
/*聲明并構(gòu)造一整數(shù)array來存儲(chǔ)欲使用的文字顏色*/
mColors = new int[]
{
Color.BLACK, Color.RED, Color.BLUE,
Color.GREEN, Color.MAGENTA, Color.YELLOW
};
colornum=0;
//得到color.xml文件里的顏色
int b = getResources().getColor(R.drawable.blue);//得到配置文件里的顏色
mButton.setTextColor(b);
/*使用setOnClickListener讓按鈕聆聽事件*/
mButton.setOnClickListener(new View.OnClickListener()
{
/*使用onClick讓用戶點(diǎn)下按鈕來驅(qū)動(dòng)變動(dòng)文字顏色*/
public void onClick(View v)
{
if (colornum < mColors.length)
{
mText.setTextColor(mColors[colornum]);
colornum++;
}
else
colornum=0;
}
});
fontButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mText.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"));//設(shè)置字體
fontButton.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));//用內(nèi)部支持的方式設(shè)置
}
});
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout使用白色的背景 -->
<LinearLayout
android:id="@+id/widget27"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<!--
文字使用mytext作為id使用string.xml中
的textview_str參數(shù) 預(yù)設(shè)文字顏色為按灰色
-->
<TextView
android:id="@+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textview_str"
android:textColor="@drawable/darkgray"
>
</TextView>
<!-- 按鈕以mybutton作為id使用string.xml中
的button_str參數(shù)
-->
<Button
android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_str"
>
</Button>
<Button
android:id="@+id/mybutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="字體"
>
</Button>
</LinearLayout>
color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="darkgray">#404040ff</drawable>
<drawable name="black">#000</drawable>
<drawable name="red">#ff00ff</drawable>
<drawable name="green">#0ff0ff</drawable>
<drawable name="lightgray">#c0c0c0ff</drawable>
<drawable name="white">#ffffffff</drawable>
<drawable name="yellow">#ffFF33ff</drawable>
<drawable name="blue">#00ffff</drawable>
<drawable name="gray">#808080ff</drawable>
<drawable name="magenta">#ff6699ff</drawable>
<drawable name="cyan">#66ffffff</drawable>
</resources>
strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Ex03_13</string>
<string name="app_name">Ex03_13</string>
<string name="textview_str">轉(zhuǎn)吧七彩霓虹燈</string>
<string name="button_str">按我</string>
</resources>
- Android自定義View之自定義評(píng)價(jià)打分控件RatingBar實(shí)現(xiàn)自定義星星大小和間距
- Android中讓圖片自適應(yīng)控件的大小的方法
- Android動(dòng)態(tài)添加設(shè)置布局與控件的方法
- Android中ImageView.src設(shè)置圖片拉伸、填滿控件的方法
- Android 控件設(shè)置陰影效果
- Android設(shè)置控件陰影的三種方法
- android 添加隨意拖動(dòng)的桌面懸浮窗口
- Android彈出窗口實(shí)現(xiàn)方法
- Android自定義彈出窗口PopupWindow使用技巧
- Android編程處理窗口控件大小,形狀,像素等UI元素工具類
相關(guān)文章
Android Jetpack系列之App Startup使用詳解
這篇文章主要為大家介紹了Android Jetpack系列之App Startup使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android?APP瘦身shrinkResources使用問題詳解
這篇文章主要為大家介紹了Android?APP瘦身shrinkResources使用問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄
通常手機(jī)通訊錄都會(huì)有索引欄,這篇文章主要介紹了Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄,現(xiàn)在分享給大家。2016-10-10
Eclipse開發(fā)環(huán)境導(dǎo)入android sdk的sample中的源碼
初學(xué)Android編程,Android SDK中提供的Sample代碼自然是最好的學(xué)習(xí)材料,需要的朋友可以參考下2012-12-12
android設(shè)備間實(shí)現(xiàn)無線投屏的示例代碼
Android提供了MediaProjection來實(shí)現(xiàn)錄屏,通過MediaProjection可以獲取當(dāng)前屏幕的視頻流,而視頻流需要通過編解碼來壓縮進(jìn)行傳輸,通過MediaCodec可實(shí)現(xiàn)視頻的編碼和解碼,這篇文章主要介紹了android設(shè)備間實(shí)現(xiàn)無線投屏,需要的朋友可以參考下2022-06-06
Android中ActionBar和ToolBar添加返回箭頭的實(shí)例代碼
這篇文章主要介紹了Android中ActionBar和ToolBar添加返回箭頭的實(shí)例代碼,需要的朋友可以參考下2017-09-09
一分鐘快速定位Android啟動(dòng)耗時(shí)問題
做開發(fā)除了實(shí)現(xiàn)功能,還要注重優(yōu)化,性能優(yōu)化包括的東西還是非常多的,下面這篇文章主要給大家介紹了關(guān)于如何通過一分鐘快速定位Android啟動(dòng)耗時(shí)問題的相關(guān)資料,需要的朋友可以參考下2021-07-07
Android實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀏覽器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀏覽器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Android中的popupwindow進(jìn)入和退出的動(dòng)畫效果
這篇文章主要介紹了Android中的popupwindow進(jìn)入和退出的動(dòng)畫,需要的朋友可以參考下2017-04-04

