自定义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) {
                //这里添加触发事件
            }
});
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章