RelativeLayout代码控制View小议

由于项目中列表类页面比较多,所以就想着能不能搞一个通用的ViewController去展示页面,同时页面预留下两个LinearLayout去给需要展示的其他控件留有余地。

简单来说,就是这样的:

<RelativeLayout
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical" />

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom"
        android:layout_below="@+id/top"
        android:background="@color/white"
        app:srlEnablePreviewInEditMode="false">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical" />
</RelativeLayout>

但是有时候也需要refresh在填充不满中间的区域的情况下,下面的布局能顶上去并且能保持一定的距离,话不多说,直接上代码:

		RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) refresh.getLayoutParams();
        param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        param.width = ViewGroup.LayoutParams.MATCH_PARENT;
        param.removeRule(RelativeLayout.ABOVE);
        refresh.setLayoutParams(param);
        RelativeLayout.LayoutParams param1 = (RelativeLayout.LayoutParams) bottom.getLayoutParams();
        param1.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        param1.addRule(RelativeLayout.BELOW, refresh.getId());
        bottom.setLayoutParams(param1);

总结一下,在RelativeLayout里如果希望能用代码去控制控件的位置或者间距,记得一定要删除xml里已经设定的并且会对新增属性有影响的属性,比如在这里,如果我只增加bottom的BELOW属性,而不去除refresh的ABOVE属性,就会报错:Circular dependencies cannot exist in RelativeLayout,翻译过来就是两个控件不能循环依赖,去除其中一个依赖就可以了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值