Android 使 View 高度为屏幕宽度的 50%

标签 android

我希望创建一个宽度为 match_parent 的 View ,但高度为宽度的 50%。

我该怎么做?

是通过 Activity 内的java代码来做到这一点的唯一方法吗?我必须读取屏幕宽度并以这种方式设置高度。

最佳答案

您可以使用 ConstraintLayout 实现所需的任何宽高比。

要设置 ConstraintLayout,请按照以下步骤操作:Build a Responsive UI with ConstraintLayout 。 然后你可以做这样的事情:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintDimensionRatio="h,2:1"
        app:layout_constraintHorizontal_bias="0.0" />

</android.support.constraint.ConstraintLayout>

关于Android 使 View 高度为屏幕宽度的 50%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44438996/

相关文章:

android - 手动从 Android 设备获取 LogCat

android - android webview 浏览器是否支持 html5 功能?

Android Mockito java.lang.AssertionError :

android - 如何在 Android 中以编程方式更改 ConstraintLayout subview 的约束?

java - 如何使用 Groupie RecyclerView 库和 Kotlin 删除项目时通知和更新列表

java - 如果我在项目中使用的库已停止使用,会发生什么情况,我的应用程序会崩溃吗?

java - Android Loader 是否在单独的线程中执行其工作?

android - com.google.android.gms.internal.firebase-perf.zzw 上的 NoClassDefFoundError

android - React Native - 未优化的 APK 将我的应用程序提交到 Google Play

javascript - Android WebView : parallel Loading, 布局和 Javascript