我有一个活动i,我将权重分为如下权重。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/header"
layout="@layout/header" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/footer"
android:layout_below="@+id/header"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".25"
android:orientation="vertical"
android:padding="5dp">
</View>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".75"
android:visibility="gone" />
<FrameLayout
android:id="@+id/fragment_container_frame_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:visibility="gone"
android:layout_weight=".75" />
</LinearLayout>
<include
android:id="@+id/footer"
layout="@layout/footer" />
</RelativeLayout>
第一个回收者视图是可见的,当用户点击按钮时,我隐藏了recyclerview并使框架布局可见并将片段加载到其中。
我的片段xml代码就是这个
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/header_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/chat_header_background"
android:gravity="center_vertical"
android:padding="5dp">
</RelativeLayout>
<RelativeLayout
android:id="@+id/footer_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/chat_header_background"
android:gravity="center_vertical"
android:padding="10dp">
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/footer_fragment"
android:layout_below="@+id/header_fragment"
android:background="@color/chat_body_background">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
因此,当我将片段加载到帧布局中时,片段不适合帧布局权重,它将高度作为匹配父级,并且我的视图以0.25权重被隐藏。那么我该如何解决这个问题呢。我想显示片段只在帧布局空间中可见。
答案 0 :(得分:0)
使用此片段布局代码..
强制创建根布局的线性布局并将relativeLayout嵌入其中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".75"
>
<RelativeLayout
android:id="@+id/header_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/chat_header_background"
android:gravity="center_vertical"
android:padding="5dp">
</RelativeLayout>
<RelativeLayout
android:id="@+id/footer_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/chat_header_background"
android:gravity="center_vertical"
android:padding="10dp">
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/footer_fragment"
android:layout_below="@+id/header_fragment"
android:background="@color/chat_body_background">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".25">
</RelativeLayout>
</LinearLayout>
&#13;