如何在RelativeLayout中填充空白?

时间:2018-09-10 11:54:01

标签: android xml

我有一个 RelativeLayout 作为父布局,它包含两个视图:我的自定义媒体控制器的VideoView和Custom视图。这两个视图填充了RelativeLayout的某些空间,我想通过LinearLayout填充其余空间,如您在此图片中所见:

enter image description here

这是我的xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:id="@+id/relativeLayout">

    <VideoView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/videoView"/>

    <com.example.arantik.test13.MyMediaController
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/myMediaController"
        android:layout_below="@id/videoView"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#ff0000"
        android:layout_alignParentBottom="true"
        android:layout_alignBottom="@id/myMediaController">
    </LinearLayout>

</RelativeLayout>

enter image description here

父级布局必须仅为RelativeLayout。我该怎么做?

3 个答案:

答案 0 :(得分:3)

将您的父级布局设置为LinearLayout

android:orientation="vertical"

如果您需要父布局为Relative layout。在您的Linear layout中添加以下代码。

android:layout_below="@+id/myMediaController"

答案 1 :(得分:1)

在这种情况下为什么不使用LinearLayout。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
    >

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="200dp"/>

    <com.example.arantik.test13.MyMediaController
        android:id="@+id/myMediaController"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ff0000"
        android:orientation="vertical">
    </LinearLayout>

</LinearLayout>

更新

如果要在RelativeLayout中使用此功能,请将android:layout_below="@id/myMediaController"放在LinearLayout上。

答案 2 :(得分:0)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:id="@+id/relativeLayout">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   >

    <VideoView
        android:layout_width="match_parent"
        android:layout_height="200dp"
       />

</FrameLayout>

<com.example.arantik.test13.MyMediaController
    android:id="@+id/myMediaController"
    android:layout_width="match_parent"
    android:layout_height="108dp" />



</RelativeLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="366dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:background="#ff0000"
    android:orientation="vertical">
</LinearLayout>
</RelativeLayout>