編輯:關於 Android 程式設計
自定義線性布局在項目中非常常用,本質上有兩條不同的路徑,理解它們的差別比記住語法更重要。
核心做法是:在自定義 LinearLayout 的構造函數裡用 LayoutInflater 加載一份 XML 布局,再把其中的 UI 組件提取出來做封裝,形成一個可複用的複合控件。
這種方式有個明顯限制:由於所有子元素都是運行時通過代碼動態掛載的,該控件只能作為一個獨立標籤在布局文件中聲明,無法在 XML 裡繼續往它內部插入子節點。
public class CustomLayout extends LinearLayout {
public CustomLayout(Context context) {
LayoutInflater mInflater = LayoutInflater.from(context);
View myView = mInflater.inflate(R.layout.receive, null);
addView(myView);
}
}
先定義被複用的內部布局 imagebtn.xml:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView1"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:src="@drawable/confirm" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="確定"
android:textSize="25dp" />
</LinearLayout>
接著封裝自定義控件,注意帶 AttributeSet 的構造方法才是從 XML 使用時真正會被調用的那一個:
package com.hust.customlinearlayout;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyLinearLayout1 extends LinearLayout {
private ImageView imageView;
private TextView textView;
public MyLinearLayout1(Context context) {
super(context);
}
public MyLinearLayout1(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.imagebtn, this);
imageView = (ImageView) findViewById(R.id.imageView1);
textView = (TextView) findViewById(R.id.textView1);
}
public void setImageResource(int resId) {
imageView.setImageResource(resId);
}
public void setTextViewText(String text) {
textView.setText(text);
}
}
在 activity_main.xml 中使用時,必須寫完整的包名類路徑:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<com.hust.customlinearlayout.MyLinearLayout1
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<com.hust.customlinearlayout.MyLinearLayout1
android:id="@+id/btn_error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1" />
</LinearLayout>
最後在 Activity 中綁定行為,可以看到封裝後的調用變得非常乾淨:
package com.hust.customlinearlayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private MyLinearLayout1 myLinearLayout1;
private MyLinearLayout1 myLinearLayout2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myLinearLayout1 = (MyLinearLayout1) findViewById(R.id.btn_right);
myLinearLayout2 = (MyLinearLayout1) findViewById(R.id.btn_error);
myLinearLayout1.setTextViewText("確定");
myLinearLayout2.setTextViewText("取消");
myLinearLayout1.setImageResource(R.drawable.confirm);
myLinearLayout2.setImageResource(R.drawable.cancle);
myLinearLayout1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "點擊的正確按鈕", 1).show();
}
});
myLinearLayout2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "點擊的錯誤按鈕", 1).show();
}
});
}
}
另一種思路是不依賴任何 XML,直接在 Java 代碼裡 new 出所有子 View,再通過 addView() 掛到自定義的容器上。這種方式靈活度更高,適合結構簡單、內容完全由數據驅動的場景。
private class SpeechView extends LinearLayout {
private TextView mTitle;
private TextView mDialogue;
public SpeechView(Context context, String title, String words) {
super(context);
this.setOrientation(VERTICAL);
// Here we build the child views in code. They could also have
// been specified in an XML file.
mTitle = new TextView(context);
mTitle.setText(title);
addView(mTitle, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
mDialogue = new TextView(context);
mDialogue.setText(words);
addView(mDialogue, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
/** Convenience method to set the title of a SpeechView */
public void setTitle(String title) {
mTitle.setText(title);
}
/** Convenience method to set the dialogue of a SpeechView */
public void setDialogue(String words) {
mDialogue.setText(words);
}
}
兩種方式的取捨很清晰:結構固定、樣式豐富的複合控件用第一種,把樣式交給 XML 管理;結構簡單、需要動態增減子項的場景用第二種,把控制權握在代碼手裡。
從零開始:用 Android Studio 創建並跑通你的第一個項目
一、創建 HelloWorld 項目 幾乎所有編程語言的第一個程序都是 Hello World,這個傳統從上世紀 70 年代流傳至今,早已成為編程界的經典儀式。這次我們就
Android 人臉辨識技術概覽
FaceDetector 的基本能力Android 早期提供的人臉辨識能力主要建立在 FaceDetector 之上。它可以用來偵測圖片中的人臉位置,並回傳人臉資訊。底層
Android 性能優化入門:TraceView 工具篇
Android 性能優化是開發中經常遇到的問題。所謂「工欲善其事,必先利其器」,本文先從工具談起,介紹 Android 性能優化中最得力的幫手——TraceView。 T
Android 日曆控件:KCalendar 使用與自訂
日曆控件的效果在需要選擇日期的應用中,自訂日曆控件能提供更貼近品牌風格的體驗。KCalendar 這類控件通常支援月份切換、日期標記、點擊監聽與彈出視窗整合。以 Popu