編輯:Android 資訊
Button 就是按鈕,是 Android 中應用最多的組件之一。它有兩種用法:一是在 XML 中配置,二是直接在程序中操作。
在 XML 布局中,常會遇到這些單位:
px:屏幕像素點
in:英寸
mm:毫米
pt:磅,1/72 英寸
dp:基於 density 的抽象單位,160dpi 屏幕上 1dp=1px
dip:等同於 dp
sp:與 dp 相似,但會隨用戶的字體大小偏好縮放
建議文本用 sp,其餘用 dip。
例 1:在 XML 中布局,這樣設置後續修改更方便,也符合 MVC 模式。
main.xml 布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://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="這是個Button示例" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/button1" android:text="按鈕一" /> <Button android:layout_width="150dip" android:layout_height="30dip" android:background="#aa00aa" android:textColor="#00aa00" android:layout_gravity="center" android:id="@+id/button2" android:text="按鈕二" /> </LinearLayout>
其中:button1 的 layout_width 為 fill_parent(充滿父控件)、layout_height 為 wrap_content(充滿內容);button2 指定了寬高 150dip×30dip、背景色、文本色,並用 layout_gravity="center" 讓控件居中(注意 android:gravity="center" 是讓文本在控件內居中)。
TestButton.java 源碼:
package org.loulijun.button; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.Button; import android.widget.Toast; public class TestButton extends Activity { /** Called when the activity is first created. */ private Button btn1,btn2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn1=(Button)findViewById(R.id.button1); btn2=(Button)findViewById(R.id.button2); //為控件設置監聽 btn1.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Toast toast=Toast.makeText(TestButton.this, "你點擊了按鈕"+btn1.getText().toString(), Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP,0,150); toast.show(); } }); btn2.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub TestButton.this.finish(); } }); } }
運行結果:


點擊按鈕一會彈出提示,點擊按鈕二則退出程序。
當然,也可以直接在程序中調用相應方法設置 Button 的屬性:
public class TestButton extends Activity{ private Button btn1,btn2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn1=(Button)findViewById(R.id.button1); btn2=(Button)findViewById(R.id.button2); btn1.setWidth(150); btn2.setWidth(100); btn1.setText("按鈕一"); btn2.setText("安妮二"); btn1.setTextColor(Color.GREEN); btn2.setTextColor(Color.RED); btn1.setTextSize(30); btn2.setTextSize(20); btn2.setBackgroundColor(Color.RED); } }
通過 setWidth/setHeight、setText、setTextColor、setTextSize、setBackgroundColor 等方法,即可在代碼中動態控制按鈕的寬高、文本、顏色與字號等屬性。
Android AVD 的建立、啟動與常見錯誤排除
AVD 全名 Android Virtual Device,也就是 Android 的虛擬裝置,是模擬器辨識和執行的單位。要讓 Android 跑起來必須先建立 AVD,
AndroidEventBus 的高階玩法:解耦、退出應用與自定義事件處理器
顧名思義,AndroidEventBus 是一個面向 Android 平台的事件總線框架。它簡化了 Activity、Fragment、Service 之間的交互,大幅降
Android NDK 開發 Crash 錯誤定位
前言 Android 中的崩潰大致可分為未捕獲例外、ANR 與原生層崩潰。Java 層例外通常可從 logcat 堆疊追蹤,但 NDK 造成的閃退往往較難定位。 NDK
深入理解 Android Matrix:從 3×3 矩陣到圖形變換
在 Android 開發中,Matrix 是一個非常強大的工具。它可以用來做平移、縮放、旋轉,也能應用在動畫與圖片處理。本文從矩陣結構出發,說明 Matrix 為什麼是