Android 平常开发中,两个很是实用的布局技巧

Android 布局容器、经常使用控件和属性,相信每一个开发者都能滚瓜烂熟,开发排版 layout 时也能适当取舍。可是,本文中介绍的这两个常见的设计场景,其特殊的实现技巧可能你真的未曾用过。php

RelativeLayout 水平居中等分


设计场景:android

看到这样的效果,可能你会不假思索地选择 LinearLayout 容器,同时分配 children 的 weight 属性。不错,这样实现确实很简单。可是,一般界面上还有其余元素,父容器通常使用的是 RelativeLayout ,若是再选择使用一层 LinearLayout 包裹这两个 Button 的话,无疑会额外增长视图层次(View Hierarchy),加大性能渲染压力。其实,大可没必要这样作,RelativeLayout 也能让两个 children 水平居中等分宽度。微信

实现方式以下:布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" >

    <Button android:id="@+id/world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/hello" android:layout_toRightOf="@+id/view" android:text="First" />

    <View android:id="@+id/view" android:layout_width="0dp" android:layout_height="1dp" android:layout_centerHorizontal="true" />

    <Button android:id="@+id/hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/view" android:text="Second" />

</RelativeLayout>复制代码

ScrollView 内容适配技巧


设计场景:性能

简单说明一下,这种界面,或者说相似的界面很常见,中间内容长度不固定,或者说相同内容在不一样设备上屏幕占比不一致,致使底部操做按钮位置也不尽相同。好比,这个界面的产品重点是但愿用户可以读完协议内容,而后再作出下一步操做。也就是说,你不能作成这个样子:spa

能够对比着前面那张图看一下,产品但愿的实现效果是:当屏幕空间足够大(或者说中间内容较少)时,操做按钮显示在屏幕底部;当屏幕空间不足(或者说中间内容较多)时,之内容显示为主,操做按钮位于屏幕以外,向上滚动方可进入屏幕可见范围,而后进行下一步操做。设计

理解需求以后,咱们再来看一下实现方式:code

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true">

    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">

        <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="8dp" android:text="用户协议" android:textAppearance="?android:attr/textAppearanceMedium" />

        <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginTop="8dp" android:background="@color/colorPrimary" />

        <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:padding="16dp" android:text="@string/content" />

        <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:drawable/bottom_bar" android:gravity="center_vertical">

            <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="赞成" />

            <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="拒绝" />
        </LinearLayout>

    </LinearLayout>

</ScrollView>复制代码

注意两个地方,第一个是设置 ScrollView 的 android:fillViewport="true" 属性,保证内容占据整个屏幕空间;第二个地方是,中间部分,也就是例子中的 TextView 巧妙运用 android:layout_weight 属性,实现高度上的自适应,保证后面的操做按钮部分可以按照咱们指望的要求正确展现。cdn

好好领会一下,相信咱们必定可以有所收获。而且你会发现,实际开发过程当中可以运用上述两个布局技巧的地方会有不少。固然,读完此文,若是你还用过其余比较特别的技巧的话,不妨留言交流一下。xml

欢迎关注个人微信公众号


安卓笔记侠:专一于 Android 开发中的原创笔记记录。

独立博客:yifeng.studio/

相关文章
相关标签/搜索