Android通过在布局文件中设置include实现xml布局的复用

本文介绍了如何在Android开发中利用<include/>标签在XML布局文件中实现组件的复用,以title_item布局为例,详细阐述了具体步骤和实践效果。

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

效果如图:


需要的布局文件title_item:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dip"
    android:background="@android:color/holo_red_light" >

    <ImageView
        android:id="@+id/title_image_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="5dip"
        android:paddingRight="5dip"
        android:paddingTop="5dip"
        android:src="@drawable/back" />

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="记事本"
        android:textSize="20sp"
        android:textColor="@android:color/black" />

    <ImageView
        android:id="@+id/title_image_setting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingBottom="5dip"
        android:paddingLeft="5dip"
        android:paddingTop="5dip"
        android:src="@drawable/more" />

</RelativeLayout>

具体实现的xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.mytest.MainActivity" >

    <include layout="@layout/title_item" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:maxHeight="40dip"
            android:maxWidth="40dip"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/image"
            android:layout_toRightOf="@+id/image"
            android:text="@string/hello_world" />

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="点击弹出Menu" />
    </RelativeLayout>

</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值