Android 自定义组合布局

本文介绍了一种自定义组合控件的实现方法,通过继承RelativeLayout并定义布局,实现了SettingItemClickView组件。该组件允许设置标题和描述,并通过XML自定义属性进行配置。

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

package com.itheima.mobilesafe66.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itheima.mobilesafe66.R;

/**
 * 自定义组合控件
 * 
 * 1. 写一个类继承RelativeLayout(ViewGroup) 2. 写布局文件 3.
 * 将布局添加到RelativeLayout中(initView方法) 4. 增加api 5. 自定义属性(1. values/attrs.xml, 2.
 * 声明命名空间 , 3.在自定义view中配置属性, 4. 在自定义view中加载属性值 )
 * 
 * @author Kevin
 * 
 */
public class SettingItemClickView extends RelativeLayout {

    private TextView tvTitle;
    private TextView tvDesc;

    public SettingItemClickView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    public SettingItemClickView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public SettingItemClickView(Context context) {
        super(context);
        initView();
    }

    /**
     * 初始化布局
     */
    private void initView() {
        View child = View.inflate(getContext(),
                R.layout.setting_item_click_view, null);// 初始化组合控件布局

        tvTitle = (TextView) child.findViewById(R.id.tv_title);
        tvDesc = (TextView) child.findViewById(R.id.tv_desc);

        this.addView(child);// 将布局添加给当前的RelativeLayout对象
    }

    /**
     * 设置标题
     * 
     * @param title
     */
    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    /**
     * 设置表述
     * 
     * @param desc
     */
    public void setDesc(String desc) {
        tvDesc.setText(desc);
    }
}

1、对自定义控件进行代码填充
sicLocation = (SettingItemClickView) findViewById(R.id.sic_location);
sicLocation.setTitle(“归属地提示框位置”);
sicLocation.setDesc(“设置归属地提示框的显示位置”);

2、在values的attrs中设置组合布局属性
(1)定义

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="SettingItemView">
        <attr name="title" format="string" />
        <attr name="desc_on" format="string" />
        <attr name="desc_off" format="string" />
    </declare-styleable>

</resources>

(2)引用 activity_setting.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.mobilesafe66"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        style="@style/TitleStyle"
        android:text="设置中心" />

    <com.itheima.mobilesafe66.view.SettingItemView
        android:id="@+id/siv_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        itheima:desc_off="自动更新已关闭"
        itheima:desc_on="自动更新已开启"
        itheima:title="自动更新设置" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值