Android Studio布局问题:模拟器中ListView下移,求适配布局方案
解决ListView下移问题的两种方案
方案一:修正RelativeLayout布局
你当前的ListView布局属性存在冲突:layout_centerVertical会让它垂直居中,alignParentBottom又强制它贴底部,而且没和顶部的标题控件绑定位置,导致下移。修改步骤如下:
- 移除ListView的
layout_centerVertical、alignParentBottom属性 - 添加
layout_below="@+id/selected_item_textview",让ListView紧跟标题栏下方 - 把ListView的
layout_width设为match_parent,layout_height设为match_parent,填充剩余屏幕空间 - 移除TextView没用的
layout_weight(RelativeLayout不支持该属性)
修改后的完整XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/selected_item_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginStart="68dp" android:layout_marginTop="19dp" android:text="공지사항" android:textColor="@color/black" android:textSize="20sp" android:textStyle="bold" /> <ImageButton android:id="@+id/imageButton" style="?android:borderlessButtonStyle" android:layout_width="71dp" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginStart="-7dp" android:layout_marginTop="10dp" app:srcCompat="@drawable/baseline_arrow_back_24" /> <Button android:id="@+id/btn_noti" style="?android:borderlessButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" android:layout_marginTop="12dp" android:layout_marginEnd="17dp" /> <ListView android:id="@+id/lvw" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/selected_item_textview" android:layout_alignParentStart="true" android:layout_marginStart="-1dp" /> </RelativeLayout>
方案二:改用LinearLayout布局(更简洁)
换成垂直方向的LinearLayout,顶部导航栏用水平LinearLayout包裹,ListView通过layout_weight="1"占满剩余空间,彻底避免RelativeLayout的位置冲突问题:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 顶部导航栏 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingVertical="10dp" android:gravity="center_vertical"> <ImageButton android:id="@+id/imageButton" style="?android:borderlessButtonStyle" android:layout_width="71dp" android:layout_height="wrap_content" android:layout_marginStart="-7dp" app:srcCompat="@drawable/baseline_arrow_back_24" /> <TextView android:id="@+id/selected_item_textview" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginStart="68dp" android:text="공지사항" android:textColor="@color/black" android:textSize="20sp" android:textStyle="bold" /> <Button android:id="@+id/btn_noti" style="?android:borderlessButtonStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="17dp" /> </LinearLayout> <!-- 公告列表 --> <ListView android:id="@+id/lvw" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>
额外提示
- 文字大小建议用
sp替代dp,适配不同系统字体缩放 - 尽量避免使用负margin(比如
layout_marginStart="-7dp"),容易导致控件超出屏幕范围
内容的提问来源于stack exchange,提问作者kimzero
相关产品推荐
相关产品推荐

