RelativeLayout在ViewPager中的一个bug

本文探讨了在使用ViewPager作为容器时,遇到RelativeLayout高度始终为全屏的问题,并提供了将RelativeLayout替换为LinearLayout的解决方案,以确保布局显示正确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RelativeLayout在ViewPager中的一个bug

 

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/bg_list_item"
    android:gravity="center_horizontal"
    android:orientation="vertical">
 
   <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
 
        <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
           android:text="@string/app_name"/>
   </RelativeLayout>
 
   <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center_horizontal">
 
        <ImageView
           android:id="@+id/image_logo"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:background="@drawable/ic_launcher"
           android:scaleType="fitCenter"/>
 
        <RelativeLayout
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentRight="true"
           android:background="@drawable/bg_like">
 
            <ImageView
               android:id="@+id/image_like"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_centerHorizontal="true"
               android:background="@drawable/card_like"/>
 
            <TextView
               android:id="@+id/text_star"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignEnd="@id/image_like"/>
        </RelativeLayout>
 
        <TextView
           android:id="@+id/text_title"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentBottom="true"
           android:text="@string/app_name"/>
   </LinearLayout>
 
   <RelativeLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
 
        <ListView
            android:id="@+id/content_list"
           android:layout_width="match_parent"
           android:layout_height="wrap_content">
        </ListView>
   </RelativeLayout>
 
</LinearLayout>


 

在ViewPager的子view中,如果配置如上,在第二层中是RelativeLayout,尽管配置的是wrap_content,会发现RelativeLayout的Height始终都是全屏的。

 

如果改成LinearLayout,一切正常


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值