Android:利用Style和include标签来减少布局中的重复代码

本文介绍了如何在Android开发中通过使用Style和include标签来减少XML布局中的重复代码。首先,通过创建样式(style)在styles.xml文件中存储控件的公共属性,然后在布局中引用这些样式,以此替代重复的属性设置。此外,文章还讲解了如何使用include标签将重复的View组件提取到单独的xml文件中,并在需要的地方进行引用,进一步简化布局代码。
摘要由CSDN通过智能技术生成

  XML布局中我们可能会遇到需要重复放置多个相同控件的情况。

 

好比上图的布局,如果按照常规来做,我们会在布局中放置三个Button,外加两个View作为分界线,其代码如下:

<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">

    
    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:gravity="center"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:text="1"
        />
    
    <View 
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#BFBFBF"/>
    
    
    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:gravity="center"
        a
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值