动态增加和减少相对布局

时间:2017-03-24 09:01:12

标签: android android-recyclerview relativelayout

动态增加和减少相对布局,我希望在从屏幕底部弹出回收器视图时缩小相对布局的大小,并且在使用它之后,屏幕将恢复到正常大小(在回收器视图中即可显示图标以便放置相对观点)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    android:layout_weight="1">

////////tool bar
    <include layout="@layout/toolbar" />


    <RelativeLayout
        android:id="@+id/rel1"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        android:layout_below="@+id/toolbar"
        android:layout_weight="1">

         <com.cmlibrary.CircleMenu
            android:id="@+id/circle_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="180dp"
            android:layout_marginTop="180dp"

            />
     </RelativeLayout>
     <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_below="@+id/rel1"
        android:padding="5dp" />
    </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

更好的方法是根据需要显示和隐藏视图的可见性。但是如果你想增加和减小尺寸,那么下面的代码会帮助你。

 private void layoutParams() {
    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) relativeLayout.getLayoutParams();
    ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
    /* When recyclerView shows*/
    layoutParams.height = layoutParams.height - params.height;
    relativeLayout.setLayoutParams(layoutParams);

    /* When recyclerView hidden*/
    layoutParams.height = RelativeLayout.LayoutParams.MATCH_PARENT;
    relativeLayout.setLayoutParams(layoutParams);
}

希望这会有所帮助。