很多時候android自訂控制項並不能滿足需求,如何做呢?很多方法,可以自己繪製一個,可以通過繼承基礎控制項來重寫某些環節,當然也可以將控制群組合成一個新控制項,這也是最方便的一個方法。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv"
android:src="@drawable/confirm"
android:paddingTop="5dip"
android:paddingBottom="5dip"
android:paddingLeft="40dip"
android:layout_gravity="center_vertical"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="確定"
android:textColor="#000000"
android:id="@+id/tv"
android:layout_marginLeft="8dip"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
這個xml實現一個左圖右字的布局,接下來寫一個類繼承LinearLayout,匯入剛剛的布局,並且設定需要的方法,從而使的能在代碼中控制這個自訂控制項內容的顯示。代碼如下:
import android.content.Context;
04.import android.util.AttributeSet;
05.import android.view.LayoutInflater;
06.import android.widget.ImageView;
07.import android.widget.LinearLayout;
08.import android.widget.TextView;
09.
10.public class ImageBt extends LinearLayout {
11.
12. private ImageView iv;
13. private TextView tv;
14.
15. public ImageBt(Context context) {
16. this(context, null);
17. }
18.
19. public ImageBt(Context context, AttributeSet attrs) {
20. super(context, attrs);
21. // 匯入布局
22. LayoutInflater.from(context).inflate(R.layout.custombt, this, true);
23. iv = (ImageView) findViewById(R.id.iv);
24. tv = (TextView) findViewById(R.id.tv);
25.
26. }
27.
28. /**
29. * 設定圖片資源
30. */
31. public void setImageResource(int resId) {
32. iv.setImageResource(resId);
33. }
34.
35. /**
36. * 設定顯示的文字
37. */
38. public void setTextViewText(String text) {
39. tv.setText(text);
40. }
41.
42.}
第三步,在需要使用這個自訂控制項的layout中加入這控制項,只需要在xml中加入即可。方法如下:
01.<RelativeLayout
02. android:orientation="horizontal"
03. android:layout_width="fill_parent"
04. android:layout_height="wrap_content"
05. android:layout_gravity="bottom"
06. >
07. <com.notice.ib.ImageBt
08. android:id="@+id/bt_confirm"
09. android:layout_height="wrap_content"
10. android:layout_width="wrap_content"
11. android:layout_alignParentBottom="true"
12. android:background="@drawable/btbg"
13. android:clickable="true"
14. android:focusable="true"
15. />
16. <com.notice.ib.ImageBt
17. android:id="@+id/bt_cancel"
18. android:layout_toRightOf="@id/bt_confirm"
19. android:layout_height="wrap_content"
20. android:layout_width="wrap_content"
21. android:layout_alignParentBottom="true"
22. android:background="@drawable/btbg"
23. android:clickable="true"
24. android:focusable="true"
25. />
26. </RelativeLayout>
注意的是,控制項標籤使用完整的類名即可。為了給按鈕一個點擊效果,你需要給他一個selector背景,這裡就不說了。
最後一步,即在activity中設定該控制項的內容。當然,在xml中也可以設定,但是只能設定一個,當我們需要兩次使用這樣的控制項,並且顯示內容不同時就不行了。在activity中設定也非常簡單,我們在ImageBt這個類中已經寫好了相應的方法,簡單調用即可。代碼如下:
01.public class MainActivity extends Activity {
02.
03. private ImageBt ib1;
04. private ImageBt ib2;
05.
06. /** Called when the activity is first created. */
07. @Override
08. public void onCreate(Bundle savedInstanceState) {
09. super.onCreate(savedInstanceState);
10. setContentView(R.layout.login);
11.
12. ib1 = (ImageBt) findViewById(R.id.bt_confirm);
13. ib2 = (ImageBt) findViewById(R.id.bt_cancel);
14.
15. ib1.setTextViewText("確定");
16. ib1.setImageResource(R.drawable.confirm);
17. ib2.setTextViewText("取消");
18. ib2.setImageResource(R.drawable.cancel);
19.
20. ib1.setOnClickListener(new OnClickListener() {
21.
22. @Override
23. public void onClick(View v) {
24. //在這裡可以實現點擊事件
25. }
26. });
27.
28. }
29.}