1. 程式人生 > >Android—TextView 背景顏色與背景圖片設定

Android—TextView 背景顏色與背景圖片設定

Android TextView 背景顏色與背景圖片設定,android textview 控制元件,android textview 背景,

android textview 圖片,android textview 顏色,android textview 元件,android textview background 。

設定文字顏色

TextView textView = (TextView) findViewById(R.id.textview1); 
// 使用實際的顏色值設定字型顏色
textView.setTextColor(android.graphics.Color.RED); 
設定背景顏色

有三種方法:

setBackgroundResource:通過顏色資源ID設定背景色。
setBackgroundColor:通過顏色值設定背景色。
setBackgroundDrawable:通過Drawable物件設定背景色。


下面分別演示如何用這3個方法來設定TextView元件的背景


setBackgroundResource方法設定背景:

以下為引用內容: 
textView.setBackgroundResource(R.color.background);  

setBackgroundColor方法設定背景:
textView.setBackgroundColor(android.graphics.Color.RED);

setBackgroundDrawable方法設定背景:
Resources resources=getBaseContext().getResources(); 
Drawable drawable=resources.getDrawable(R.color.background); 
textView.setBackgroundDrawable(drawable); 
設定背景圖片


1、將背景圖片放置在 drawable-mdpi 目錄下,假設圖片名為 bgimg.jpg 。


2、main.xml 檔案

<EditText
   android:layout_width="fill_parent"
   android:layout_height="wrap_content" 
   android:background="@drawable/bgimg"
/>