gpt4 book ai didi

android - Android 的纵横比等效?

转载 作者:行者123 更新时间:2023-11-29 14:59:46 27 4
gpt4 key购买 nike

我想添加一个前导/尾随边距为 12dp 且纵横比为屏幕宽度 4:1 的 View 。来自 iOS 背景,我希望得到这样的结果:

enter image description here

现在我想对 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>

为了获得预期的比例,我必须在此处更改 View 的哪些高度?

感谢您的帮助。

最佳答案

您可以使用 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>

Output

关于android - Android 的纵横比等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49811826/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com