学会巧用style文件偷懒


有时候在布局的代码中会出现很多的重复代码,一句两句估计还是我们的承受范围之内的。要是在大工程中那

估计你就要疯了,这里我们就需要用到style属性来消除重复的代码。


首先我们看看下面的一个button布局:


<RelativeLayout
    android:id="@+id/relative"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/bt_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/butoon1"/>

    <Button
        android:id="@+id/bt_button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/bt_button1"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/butoon1"/>

    <Button
        android:id="@+id/bt_button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/bt_button2"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/butoon1"/>

    <Button
        android:id="@+id/bt_button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/bt_button3"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/butoon1"/>
</RelativeLayout>

细心的你就会发现每个Button都有下面这些代码,你是否会感到繁琐

 android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"


这里的话如果我们在res的values文件下的styles.xml文件设置style属性就可以省去很多重复的代码

<resources>
    
    <style name="styleMode">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_centerVertical">true</item>
        <item name="android:layout_marginLeft">20dp</item>
        <item name="android:layout_marginTop">20dp</item>
    </style>
</resources>


再来看看我们的button布局所需要的代码

<RelativeLayout
    android:id="@+id/relative"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/bt_button1"
        style="@style/styleMode"
        android:text="@string/butoon1"/>

    <Button

        android:id="@+id/bt_button2"
        style="@style/styleMode"
        android:layout_below="@+id/bt_button1"
        android:text="@string/butoon1"/>

    <Button

        android:id="@+id/bt_button3"
        style="@style/styleMode"
        android:layout_below="@+id/bt_button2"
        android:text="@string/butoon1"/>

    <Button
        android:id="@+id/bt_button4"
        style="@style/styleMode"
        android:layout_below="@+id/bt_button3"
        android:text="@string/butoon1"/>
</RelativeLayout>

是不是感觉比最上面的代码少了很多,在大工程中这个的设置可是非常用用的。






<RelativeLayout
    android:id="@+id/relative"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/bt_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/butoon1"/>

    <Button
        android:id="@+id/bt_button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/bt_button1"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/butoon1"/>

    <Button
        android:id="@+id/bt_button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/bt_button2"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/butoon1"/>

    <Button
        android:id="@+id/bt_button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/bt_button3"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/butoon1"/>
</RelativeLayout>

当你看到每个Button都有下面这些代码的时候你是否会感到繁琐

 android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/bt_button3"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"


这里的话如果我们在res的values文件下的styles.xml文件设置style属性就可以省去很多重复的代码

<resources>
    
    <style name="styleMode">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_centerVertical">true</item>
        <item name="android:layout_marginLeft">20dp</item>
        <item name="android:layout_marginTop">20dp</item>
    </style>
</resources>


再来看看我们的button布局所需要的代码

<RelativeLayout
    android:id="@+id/relative"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/bt_button1"
        style="@style/styleMode"
        android:text="@string/butoon1"/>

    <Button

        android:id="@+id/bt_button2"
        style="@style/styleMode"
        android:layout_below="@+id/bt_button1"
        android:text="@string/butoon1"/>

    <Button

        android:id="@+id/bt_button3"
        style="@style/styleMode"
        android:layout_below="@+id/bt_button2"
        android:text="@string/butoon1"/>

    <Button
        android:id="@+id/bt_button4"
        style="@style/styleMode"
        android:layout_below="@+id/bt_button3"
        android:text="@string/butoon1"/>
</RelativeLayout>

是不是感觉比最上面的代码少了很多,在大工程中这个的设置可是非常用用的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值