ScrollView内容适配及RelativeLayout 水平居中等分和添加等分分割线

好久没写博客了!闲来试着写写博客!

以前写水平居中等分时,一般都使用LinearLayout的weight属性,但如果多一层,就要多个视图层次,那如何用RelativeLayout实现?请看代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
        <Button
            android:id="@+id/world"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/hello"
            android:layout_toRightOf="@+id/view"
            android:text="FIRST"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    **<View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:id="@+id/view"
        android:layout_centerHorizontal="true"/>**
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/hello"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/view"
        android:text="SECOND"/>
</RelativeLayout>

其中View的属性,layout_width设计为0dp和layout_centerHorizontal="true"是相当重要 的
效果图:这里写图片描述

ScrollView内容适配
由于ScrollView中内容长度不固定。正常情况如下,在中间内容比较少时,底部按钮默认底部显示,当中间内容非常多的时候,底部按钮隐藏,在滑动到最后的时候,底部按钮显示
其代码实现:

<?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:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:gravity="center"
            android:padding="8dp"
            android:text="用户协议"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <View
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="16dp"
            android:background="@color/colorPrimary"
            android:layout_width="match_parent"
            android:layout_height="1dp"/>
        <TextView
            android:layout_weight="1"
            android:padding="16dp"
            android:text="1、人生是公平的,有付出就有收获。如果你想要什么,那就勇敢地去追求,不要管别人是怎么想的,因为这就是实现梦想的方式。最大的淡定,是看透人生以后依然能够热爱生活。拿得起,有担当,也放得下。

  2、要知道一个男人爱不爱你,那还不容易吗?爱你的那个人会给你尊严。什么样的尊严?他让你觉得自己高贵。

  3、一把钥匙打不开千把锁,无论是还是都煲不出包治百病的心灵鸡汤。如果不聆听个体内心的真实需要,也分析不出解决情感问题的有效方法。真正能给人以启迪的人生感悟是经过生活的积累和沉淀而产生的,没有人能量产批发。

  4、有些人不会忘,由于不舍得;有些人必需要忘,因为不值得。

  5、真正了解你的人,是当别人都对你的笑容信以为真的时候,看得见你眼里的痛的人。

  6、记忆有是三种面孔,你的一面,我的一面,和那个最真实的一面。

  7、也许你觉得生活不够美好,也许你觉得爱情不够甜蜜,也许你觉得前途不够光明。

  8、你看,这么多人,这么大的世界。我遇到了你,你也遇到了我,挺好。

  9、久病才知谁爱你,深醉便知你爱谁"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <LinearLayout
            android:gravity="center_vertical"
            android:background="@android:drawable/bottom_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <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>

其效果图为—不占满屏的:
这里写图片描述

占满屏的:
这里写图片描述

其特别需要注意几点:
1,ScrollView只能有一个子控件,其子控件下可以有多个子控件!
2,必须设置 ScrollView的android:filViewport=“true” 保证内容占据整个屏幕
3,设置内容及TextView的 android:layout_weight 实现自适应
layout_weight=“1” android:layout_width=“match_parent”

第三条:去掉View 添加等分分割线
主要用到三个属性:
divider -用来定义一个drawable或者color作为分割线

showDividers -指定分割线在哪里显示,它们可以显示在开始位置,中间,末尾或者选择不显示

dividerPadding -给divider添加padding
代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:background="@android:color/white"
              android:divider="@drawable/divider_horizen"
              android:dividerPadding="5dp"
              android:showDividers="middle">
    <TextView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="0.5"
        android:gravity="center"
        android:text="取消"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="0.5"
        android:gravity="center"
        android:text="确认"/>
</LinearLayout>

效果图如下:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值