【问题标题】:Making a RelativeLayout fill the screen exactly使 RelativeLayout 完全填满屏幕
【发布时间】:2023-12-31 16:14:01
【问题描述】:

我有一个带有其他几个子项(其他布局、图像视图和按钮)的 RelativeLayout。

所有内部布局/视图都适应相关的 RelativeLayout。我可以让这个 RelativeLayout 完全填满屏幕吗?

编辑:我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/container"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center_vertical|center_horizontal"
   android:layout_gravity="center_vertical" >

 <ScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:gravity="center_vertical"
    android:orientation="vertical" >

 <RelativeLayout
    android:id="@+id/start_activity_relLayout_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/start_activity_relLayout_container_bottom"
    android:gravity="center_horizontal"
    android:layout_gravity="center_vertical" >

 <RelativeLayout
    android:id="@+id/relLayout_dialogfragment_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linLayout_imageview_container"
    android:gravity="center_vertical" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/tutorial_thirdview"
        android:text="@string/button_servicerequirement" />

</RelativeLayout>
 </RelativeLayout>

  <RelativeLayout
    android:id="@+id/start_activity_relLayout_container_bottom"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_marginTop="@dimen/two">

  <LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true">

     <Button
        android:id="@+id/image_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_cancel"
        style="@style/button_standard_bright_smaller" 
        android:layout_marginRight="3dp"/>
     <Button
        android:id="@+id/image_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_ok" 
        android:layout_marginLeft="3dp"/>
       </LinearLayout>
   </RelativeLayout>
  </LinearLayout>  
 </ScrollView>  
</RelativeLayout>

编辑 2: 我正在动态设置图像视图。

【问题讨论】:

  • 在此处显示您的 xml 文件
  • @PiYusH GuPtA 我刚刚做了 :)
  • 根据您的 xml,父 RelativeLayout 确实填满了整个屏幕。
  • 它比屏幕填得更多,这就是问题^^我需要它来完全填满屏幕。

标签: android android-relativelayout fullscreen


【解决方案1】:

您是否尝试在 xml 布局文件中将 RelativeLayout 的 layoutWidthlayoutHeight 属性设置为 match_parent

【讨论】:

  • 是的,不幸的是这没有帮助。我还有一个 ScrollView 作为 RelativeLayout 中的下一个子项 - 这可能是问题吗?
  • 你能发布你的 xml 布局吗?
  • fill_parent 自 API 级别 8 起已弃用。请使用 match_parent。忽略这一点,答案应该是正确的。
  • @Leandros 不幸的是,布局填充的不仅仅是屏幕。这就是为什么我在里面有一个 ScrollView,但我不想需要 ScrollView。
  • 看看*.com/questions/7134085 那里的布局使用 ScrollView 和 android:fillViewport=true 作为根布局。