序言:此文參照官方網站:
http://developer.android.com/guide/topics/ui/controls/button.html
撰寫,部分樣例代碼引用官方代碼。樣式在本文中只做了一個簡單的敘述,詳細的論述將繼續探討。
一、創建Button的方法
方式一(Button中只顯示文字提示):
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
... />
方式二(Button中只顯示背景圖片):
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_icon"
... />
方式三(Button中文字和圖片都顯示):
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:drawableLeft="@drawable/button_icon"
... />
在這裏通過屬性:android:drawableLeft使得圖片是顯示在文字的左方的。也可以通過屬性:android:drawableTop、drawableBottom、drawableRight顯示在圖片顯示在文字的上方、下方及右方。
注:創建無邊Button,在<Button/>元素中添加屬性:style="?android:attr/borderlessButtonStyle" 可以實現。
二、添加Button事件
方式一(在樣式文件中指明):
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
通過屬性:android:onClick="方法名"實現。但必須注意此方法必須在調用該樣式文件的Activity中定義,定義的實現如下:
public void sendMessage(View view) {
// Do something in response to button click
}
此方法中有三個地方必須注意:
1、方法必須是public。
2、方法的返回值必須是void。
3、方法的參數有且僅有一個,而且必須是View類型的。
方式二(在運動時聲明Button或者在Fragment的子類中聲明):
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
}
});
三、Button自定義樣式
可以自定義文本顯示的大小、顏色、字體及Button的背景等。
方式一(在<Button/>元素中直接定義Button的樣式):
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:textSize="12sp"
android:textStyle="bold"
android:text="@string/hello_world"
android:background="@android:drawable/btn_dialog"/>
方式二(在單獨的樣式文件中定義):
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/CodeStyle"
android:text="@string/hello_world"/>
在工程目錄res/values/下定義樣式文件,如:style_name.xml,style_name可以任意定義,只要符合文件命名規則就行,如:custom_style.xml。
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CodeStyle" parent="android:Theme.Light">
<item name="android:textColor">#00FF00</item>
<item name="android:textSize">12sp</item>
<item name="android:textStyle">italic</item>
<item name="android:background">@android:drawable/btn_dialog</item>
</style>
</resources>
當然也可以在運行時動態通過對應的方法設置顯示屬性。
四、Button對於不同的動作顯示不同的背景
步驟一(在res/drawable/目錄下新建文件,此例文件名爲:button_custom.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/button_focused"
android:state_focused="true" />
<item android:drawable="@drawable/button_default" />
</selector>
步驟二(在<Button/>元素下調用文件):
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"
android:background="@drawable/button_custom" />
注:<selector/>元素的定義語法參照如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize=["true" | "false"]
android:dither=["true" | "false"]
android:variablePadding=["true" | "false"] >
<item
android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_hovered=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_activated=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</selector>