将imageView高度设置为其父级的3/4

时间:2016-07-10 17:46:58

标签: android android-layout

如何在布局xml文件中将imageView的高度设置为其父布局高度的3/4,全宽度和videoView占据其余部分?

以下是我的布局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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="app.com.blynq.player.MainActivity">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        />

    <VideoView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/videoView"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        />

2 个答案:

答案 0 :(得分:2)

您可以使用weight来实现目标。

将父级设置为LinearLayout并添加

android:orientation="vertical"
android:weightSum="4"

到父母和

android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="3"

给你想要做3/4的孩子。给另一个孩子,添加

android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"

因此,您的代码应如下所示:

<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:weightSum="4"
    tools:context="app.com.blynq.player.MainActivity">

    <ImageView
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="3"
        android:id="@+id/imageView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"/>

    <VideoView
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:id="@+id/videoView"
        android:layout_gravity="center"
        android:adjustViewBounds="true"/>
</LinearLayout>

答案 1 :(得分:0)

使用LinearLayout并设置权重。

<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="app.com.blynq.player.MainActivity">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:weight="3"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        />

    <VideoView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/videoView"
        android:weight="1"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        />
</LinearLayout>