Layout布局简化

这里谈到的简化布局不是教你如何使用<includ>标签、<merge>标签或者ViewStub,本篇介绍的是怎么利用<style>标签减少布局文件的代码行数。

android支持使用XML文件编写布局,我们甚至能只用标签属性初始化控件就能达到UI妹子设计的页面效果。但是,我们在标签中定义了大量属性会使xml文件看起来很臃肿,如果布局很复杂会很难看清布局的层级。

想想一个布局文件就有400多行看起来会有多费劲。举个例子,一个表单页面定义一个展示文本可能会写如下代码:

<TextView
    android:id="@+id/et_contact"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/tv_xxdz"
    android:layout_marginLeft="@dimen/padding_margin_5"
    android:layout_marginTop="29dp"
    android:layout_toRightOf="@id/tv_llr"
    android:textColor="@color/sub1_text_color"
    android:textSize="14sp"/>

仅一个控件就有10行代码,你可能在一屏里只能看到三个控件的代码,不易阅读。下面我们抽出一些代码试试。


每个控件都有宽高属性,我们可以在styles.xml中定义如下代码:

<style name="WrapWrap">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>

这个style名称由两个单词组成,第一个表示宽、第二个表示高。如果控件的宽高是不明确的,我们也可以扩展出下面的三个style:

    <style name="MatchMatch">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">match_parent</item>
    </style>
    
    <style name="WrapMatch">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">match_parent</item>
    </style>

    <style name="MatchWrap">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

表单可能要显示多个展示文本,它们的风格都是一样的,我们可以再定义一个style:

<style name="PrintTitle" parent="WrapWrap">
    <item name="android:textColor">@android:color/black</item>
    <item name="android:textSize">@dimen/print_text</item>
    <item name="android: layout_marginTop">29dp</item>
    <item name="android: layout_marginLeft">5dp</item>
</style>

使用了上面的主题后之前的TextView看起来是这样的:

<TextView
    android:id="@+id/et_contact"
    style="@style/PrintTitle"
    android:layout_below="@id/tv_xxdz"
    android:layout_toRightOf="@id/tv_llr"/>

代码行数减少了一半有没有。


如果我们大量运用这种方式,那么表单的一行可能是这样子:

屏幕快照 2017-03-03 上午12.00.38.png
<LinearLayout
    android:id="@+id/ll_actual_time"
    style="@style/CreateOrderSubItem">

    <TextView
        style="@style/CreateOrderKey"
        android:text="@string/actual_time" />

    <TextView
        android:id="@+id/tv_actual_time"
        style="@style/CreateOrderValue" />

    <ImageView style="@style/EnterArrow" />
</LinearLayout>

这样的布局代码看起来非常清晰,我的密集恐惧症也很少犯了。当然,这篇文章只是一个引子,怎么简约你的layout文件使更易阅读需要你细细琢磨。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,385评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,245评论 4 61
  • 最好的学习是自学,自主学习一般是指个体自觉主动地确定学习目标、制订学习计划、利用学习资料、监控学习过程、评价学习结...
    晴晴老师阅读 277评论 0 0
  • 频频走红毯的明星,最怕的就是撞衫,所以他们都会有一件备穿的礼服。同样品牌的衣服,在不同人身上就会有不同的效果,就像...
    不打烊实验室阅读 769评论 5 5
  • 做项目的时候需求那里要做到有三轴加速度和一个xy轴坐标底下动态显示三轴加速度数据,遇到的坑总结和记录 效果图 1....
    Karma1026阅读 3,235评论 0 2