android - 在 android ConstraintLayout 的响应式设计中有效地使用边距

标签 android responsive-design android-constraintlayout

我在 ConstraintLayout 中有一个 ImageView 并且我想给它 marginTop 以便它与屏幕顶部总共有 40% 的距离安卓设备屏幕。 我可以做些什么来获得响应式设计?

最佳答案

在您的 View 中使用 app:layout_constraintVertical_bias="0.4":

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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"
    android:orientation="vertical"
    android:padding="4dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/white_circle"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintVertical_bias="0.4"
        app:layout_constraintHorizontal_bias="0.2"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

另一种方式是:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:orientation="vertical"
    android:padding="4dp">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.4" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/white_circle"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/guideline"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

关于android - 在 android ConstraintLayout 的响应式设计中有效地使用边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57745621/

相关文章:

android - 无法为 com.android.build.gradle.internal.dsl.BaseAppModuleExtension 类型的对象设置未知属性 'useAndroidX'

android - 如何使用 Google Play 服务管理一个应用程序/游戏内的多个 'minigames'?

android - View 之间的间距相等,其中一些在 ConstraintLayout 上使用父级的百分比

android - 约束布局基线到基线与自动调整大小

android - 从测试内部下载的相同应用程序版本与从内部应用程序共享下载的相同应用程序版本之间的应用程序大小差异

android - 以编程方式更改单个 ActionBar 菜单项的 TextColor/Background

html - 调整浏览器窗口大小时使页脚中的内容居中

javascript - 如何在不使用媒体查询的情况下以特定浏览器宽度从 DOM 更改图像

html - 使用媒体查询添加/删除类

android - 以编程方式添加时左上角的 ConstraintLayout View