2016-07-10 32 views
2

如何将layoutView xml文件中imageView的高度设置为其父级布局高度的3/4,full width和videoView以占据其余部分?设置imageView高度为其父项的3/4

以下是我的布局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

您可以使用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> 
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> 
相关问题