2010年6月2日星期三

設置按鈕的文本和背景顏色

通過XML, 可以使用以下的語句:
android:background="#f00000"
android:textColor="#000000"

在運行時可以使用程序代碼:
myIconButton.setBackgroundColor(0xff505050);
myIconButton.setTextColor(0xffffffff);

設置按鈕的文本和背景顏色
設置按鈕的文本和背景顏色

main.xml
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:id="@+id/myIconButton"
android:drawableLeft="@drawable/icon"
android:background="#f00000"
android:textColor="#000000"
android:text="My Icon Button"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>


JAVA
package com.AndroidIconButton;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AndroidIconButton extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button myIconButton = (Button)findViewById(R.id.myIconButton);
myIconButton.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myIconButton.setBackgroundColor(0xff505050);
myIconButton.setTextColor(0xffffffff);

}});
}
}




沒有留言:

發佈留言