自定義RelativeLayout

這裏寫圖片描述
實現如圖白色方框所示效果。白色方框爲一個自定義的RelativeLayout,裏面包含一個TextView和一個ImageView圖片。

1、XML定義

<RelativeLayout
        android:id="@+id/layout_main_Object"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:background="@drawable/preference_single_item"
        android:clickable="true"
        android:gravity="center_vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="@string/str_ObjectManage"
            android:textColor="#000"
            android:layout_centerHorizontal="true"
            android:textSize="17sp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:scaleType="matrix"
            android:src="@drawable/mm_submenu" />
</RelativeLayout>

2、preference_single_item和mm_submenu屬性

1、preference_single_item

分別設置方框在選中、按壓和正常情況下背景的顏色。ic_preference_single_pressed和ic_preference_single_normal是經過處理可拉伸的PNG圖片。

<?xml version="1.0" encoding="UTF-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
    android:state_enabled="true" 
    android:state_selected="true" 
    android:drawable="@drawable/ic_preference_single_pressed" />
    <item 
    android:state_enabled="true" 
    android:state_pressed="true" 
    android:drawable="@drawable/ic_preference_single_pressed" />
    <item 
    android:drawable="@drawable/ic_preference_single_normal" />
</selector>

2、mm_submenu

<?xml version="1.0" encoding="UTF-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/mm_submenu_pressed" />
    <item android:state_focused="true" android:drawable="@drawable/mm_submenu_pressed" />
    <item android:state_pressed="true" android:drawable="@drawable/mm_submenu_pressed" />
    <item android:drawable="@drawable/mm_submenu_normal" />
</selector>

3、實現觸發事件

private RelativeLayout btnObject;// 工作對象管理
btnObject = (RelativeLayout) findViewById(R.id.layout_main_Object);
        btnObject.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //這裏添加觸發事件
            }
});
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章