自定义RelativeLayout

本文介绍了如何自定义一个RelativeLayout,包括XML定义,详细讲解了preference_single_item和mm_submenu属性的用法,以及如何实现触发事件的功能。通过设置不同状态下的背景颜色和使用可拉伸的PNG图片,达到理想的布局效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述
实现如图白色方框所示效果。白色方框为一个自定义的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) {
                //这里添加触发事件
            }
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值