編輯:關於 Android 程式設計
自定義控制項在 Android 中幾乎無所不在。當一個畫面需要把 ImageView、ImageButton、TextView 等控件組合在一起,而且這種組合會被重複使用時,與其每次重寫,不如封裝成一個自訂 View。這樣不僅提升開發效率,在 XML 佈局中也顯得整潔美觀。
以下屬性都定義在 values/attrs.xml 中,名稱可自由命名:
| 串接,如 reference|color例如定義一個包含多種屬性的 styleable:
<declare-styleable name = "custom_view">
<attr name = "custom_id" format = "integer" />
<attr name = "src" format = "reference" />
<attr name = "background" format = "reference" />
<attr name = "text" format = "string" />
<attr name = "textColor" format = "color" />
<attr name = "textSize" format = "dimension" />
</declare-styleable>
以一個「帶陰影背景、下方有文字說明的按鈕」為例,讓 CustomView 繼承 FrameLayout,在構造函數中透過 TypedArray 讀取我們自訂的屬性:
public class CustomView extends FrameLayout implements OnClickListener {
private CustomListener customListener = null;
private Drawable mSrc = null, mBackground = null;
private String mText = "";
private int mTextColor = 0;
private float mTextSize = 20;
private int mCustomId = 0;
private ImageButton mButtonView = null;
private TextView mTextView = null;
private ImageView mBackgroundView = null;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.custom_view);
mSrc = a.getDrawable(R.styleable.custom_view_src);
mBackground = a.getDrawable(R.styleable.custom_view_background);
mText = a.getString(R.styleable.custom_view_text);
mTextColor = a.getColor(R.styleable.custom_view_textColor, Color.WHITE);
mTextSize = a.getDimension(R.styleable.custom_view_textSize, 20);
mCustomId = a.getInt(R.styleable.custom_view_custom_id, 0);
// 建立背景、按鈕、文字三個子 View 並 addView
// ...
a.recycle();
}
public void setCustomListener(CustomListener l) {
customListener = l;
}
@Override
public void onClick(View v) {
if (customListener != null) {
customListener.onCuscomClick(v, mCustomId);
}
}
public interface CustomListener {
void onCuscomClick(View v, int custom_id);
}
}
使用時記得先在根佈局宣告命名空間前綴 xmlns:nanlus="https://schemas.android.com/apk/res/你的包名",接著就能像原生控件一樣設定自訂屬性:
<com.nanlus.custom.CustomView
android:id="@+id/custom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
nanlus:background="@drawable/background"
nanlus:custom_id="1"
nanlus:src="@drawable/style_button"
nanlus:text="按鈕1" />
public class CustomActivity extends BaseActivity implements CustomListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((CustomView) this.findViewById(R.id.custom1)).setCustomListener(this);
}
@Override
public void onCuscomClick(View v, int custom_id) {
switch (custom_id) {
case 1:
Toast.makeText(this, "hello !!!", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
透過這樣的封裝,一個複合控制項就能在 XML 中以宣告式的方式靈活配置,大幅減少重複的樣板程式碼。
Android ListView 與 Adapter 關鍵要點:getView 複用機制與布局陷阱
一、Adapter.getView():整個列表性能的命門 方法簽名很簡單: public View getView(int position, View convert
Android 中實作 TCP 與 UDP 傳輸的完整示例
從 TCP 到 UDP:Android 網路傳輸實作解析 TCP 與 UDP 是行動應用開發中最常見的傳輸層協定。在 Android 環境中,兩者的使用方式與 Java
ExoPlayer 深度筆記:從整體架構到播放原理
一、ExoPlayer 是什麼,適合用在哪些場景 ExoPlayer 是 Google 開源的應用級媒體播放方案,倉庫中同時提供了可複用的 library 與可直接跑起來
Android Studio 導入 SlidingMenu 與 FloatMath 找不到符號解法
SlidingMenu 簡介 SlidingMenu 是一個常見的第三方開源側滑選單元件,提供滑動式選單的互動方式。許多應用會使用類似設計,讓使用者可以從邊緣滑出功能列或