Android的纵横比等价物?

Android的纵横比等价物?,android,layout,aspect-ratio,Android,Layout,Aspect Ratio,我想添加一个前导/尾随边距为12dp,纵横比为屏幕宽度的4:1的视图。 来自iOS背景,我希望通过以下方式获得该结果: 现在我想对Android做同样的事情,我只关注纵横比: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" andr

我想添加一个前导/尾随边距为12dp,纵横比为屏幕宽度的4:1的视图。 来自iOS背景,我希望通过以下方式获得该结果:

现在我想对Android做同样的事情,我只关注纵横比:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_width="match_parent"
        <!-- android:layout_height="{what to do here?}" -->
        android:layout_centerInParent="true"
        android:layout_marginStart="12dp"
        android:layout_marginEnd="12dp"
        android:background="@color/my_blue_color" />

</RelativeLayout>
为了得到预期的比率,我必须在这里更改视图高度的哪些内容


谢谢您的帮助。

Ypu需要使用ConstraintLayout:

<android.support.constraint.ConstraintLayout
    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">

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="12dp"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="12dp"
        app:layout_constraintDimensionRatio="4:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

Ypu需要使用ConstraintLayout:

<android.support.constraint.ConstraintLayout
    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">

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="12dp"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="12dp"
        app:layout_constraintDimensionRatio="4:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

您可以通过使用ConstraintLayout来实现这一点

首先添加依赖项

dependencies {
    implementation'com.android.support.constraint:constraint-layout:1.0.2'
}
这里有一个例子

     <?xml version="1.0" encoding="utf-8"?>
     <android.support.constraint.ConstraintLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.burhanrashid52.unittestsample.MainActivity">


            <View
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginEnd="12dp"
                android:layout_marginStart="12dp"
                android:layout_marginTop="12dp"
                android:background="@android:color/holo_blue_dark"
                app:layout_constraintDimensionRatio="w,1:4"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


      </android.support.constraint.ConstraintLayout>

您可以通过使用ConstraintLayout来实现这一点

首先添加依赖项

dependencies {
    implementation'com.android.support.constraint:constraint-layout:1.0.2'
}
这里有一个例子

     <?xml version="1.0" encoding="utf-8"?>
     <android.support.constraint.ConstraintLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.burhanrashid52.unittestsample.MainActivity">


            <View
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginEnd="12dp"
                android:layout_marginStart="12dp"
                android:layout_marginTop="12dp"
                android:background="@android:color/holo_blue_dark"
                app:layout_constraintDimensionRatio="w,1:4"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


      </android.support.constraint.ConstraintLayout>

伟大的谢谢,太好了!非常感谢。