【问题标题】:How to set size of layouts in Android as a percentage of the sceen size如何在Android中将布局大小设置为屏幕大小的百分比
【发布时间】:2021-10-01 03:10:04
【问题描述】:

我在 Android 中有一个带有内部 ConstraintLayout 和内部线性布局的 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:id="@+id/outerConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_marginTop="@dimen/_5sdp"
        android:layout_marginBottom="@dimen/_8sdp"
        android:layout_width="@dimen/_235sdp"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:background="@drawable/rim"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.93"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.75">

    </LinearLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/innerConstraintLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/linearLayout"
        app:layout_constraintEnd_toStartOf="@+id/linearLayout"
        android:layout_marginLeft="@dimen/_10sdp"
        android:layout_marginRight="@dimen/_30sdp"
        android:background="@drawable/rim"
        android:padding="12dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/linearLayout"
        >

        <TextView
            android:id="@+id/textView_Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Inner Constrained Layout"
            android:textColor="#000000"
            android:textSize="@dimen/_10ssp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/button_okay"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_25sdp"
            android:background="@color/colorGreen"
            android:text="Okay"
            android:textAllCaps="false"
            android:textColor="#121212"
            android:textSize="@dimen/_8ssp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.553"
            app:layout_constraintStart_toStartOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

问题是两种布局在不同屏幕上显示时的比例不同(见截图):

这看起来不太好,因为根据使用的设备,布局在比例(宽度和高度)方面看起来完全不同。现在我的问题是,是否有一种方法可以将布局的宽度和高度一般设置为屏幕尺寸的百分比,从而使布局的宽度和高度的比例独立于屏幕尺寸?或者你会如何设计布局,使其宽度和高度之间的比例或多或少独立于屏幕尺寸?

感谢您的每一条评论,如果您分享您在该问题上的经验,我会很高兴?

【问题讨论】:

    标签: android layout android-constraintlayout


    【解决方案1】:

    现在我的问题是,是否有办法将布局的宽度和高度设置为屏幕大小的百分比

    可以像这样使用 constarintLayout 来完成:

    • 采取任何观点,给它android:layout_height="0dp" - 现在它会尊重你的约束,添加到它app:layout_constraintHeight_percent="0.15"

    现在视图的高度是父级大小的 15%。

    • 对于宽度android:layout_width="0dp"也可以这样做 与app:layout_constraintWidth_percent="0.5" 一起使用,现在您的视图将占据您父级大小的 50%。

    你可以像这样组合这两个:

     android:layout_width="0dp"
     android:layout_height="0dp"
     app:layout_constraintWidth_percent="0.5"
     app:layout_constraintHeight_percent="0.15"
    

    现在您的视图是父宽度的 15% 和父高度的 50%,更改这些数字,您可以根据需要控制视图大小。

    【讨论】:

    • 感谢塔米尔的回答。我工作得很好。我赞成并接受了你的回答。非常感谢您的帮助。
    【解决方案2】:
    <LinearLayout
            ...
            android:layout_width="@dimen/_235sdp"
            ...
    >
    

    我假设在@dimen/_235sdp 中您的值为235dp。如果是这样,那条线会在所有设备上为您的LinearLayout 提供235dp 的固定宽度,因此您不能指望它按百分比缩放。

    尝试使用带有百分比值的垂直Guideline 并删除所有其他固定大小:

    <?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"
        android:id="@+id/outerConstraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/guideline"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.3"/>
    
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_marginTop="@dimen/_5sdp"
            android:layout_marginBottom="@dimen/_8sdp"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="vertical"
            android:background="@android:color/holo_red_dark"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@id/guideline"
            app:layout_constraintEnd_toEndOf="parent">
    
        </LinearLayout>
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/innerConstraintLayout"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="@dimen/_10sdp"
            android:layout_marginEnd="@dimen/_30sdp"
            android:background="@android:color/holo_blue_dark"
            android:padding="12dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@id/guideline">
    
            <TextView
                android:id="@+id/textView_Name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Inner Constrained Layout"
                android:textColor="#000000"
                android:textSize="@dimen/_10ssp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <Button
                android:id="@+id/button_okay"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_25sdp"
                android:background="@android:color/holo_green_light"
                android:text="Okay"
                android:textAllCaps="false"
                android:textColor="#121212"
                android:textSize="@dimen/_8ssp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    在更新后的代码中,我添加了一条垂直引导线,将屏幕分割为 0.3 和 0.7,然后我根据引导线限制内部布局以保持比例。现在在任何设备上,您都应该看到左侧内部布局始终占用 0.3 的宽度,而右侧内部布局始终占用 0.7 的宽度。

    提示:尽量避免在ConstraintLayout 中使用ConstraintLayout,应该完全可以在不破坏布局的情况下将内部内容(即TextView、Button)限制在父ConstraintLayout 上。

    【讨论】:

    • 感谢 Dat Pham Tat 的回答。我使用来自 Tamir 的(对我而言)更简单的答案 v android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintWidth_percent="0.5" app:layout_constraintHeight_percent="0.15" 仍然我赞成你的答案,因为它包含有价值的信息。
    • 关于您的小费的一个问题。我必须使用嵌套约束布局(甚至超过 2 层;所以我有 3 次嵌套布局),因为我想在我的子布局外面有一个边缘,我想在我的布局里面有彩色区域。为此,我必须使用我必须使用多个嵌套的约束布局(请参阅我的问题stackoverflow.com/questions/68053716/…)
    • 我查看了所需的布局图片,它相当复杂,所以我现在明白为什么需要内部布局了。我添加了一个接近预期结果的答案,并且包装布局更少。不幸的是,对于彩色区域,我需要一个内部约束布局,因为没有它会很困难。
    【解决方案3】:

    我遇到了几乎相同的问题,为了解决这个问题,我将每一层分为两种模式(手机和平板电脑)(Android系统本身可以识别尺寸)并扩展两者。 这样一来,我的工作就更有规律了,对我来说很容易改变下一个模式。

    我的解决方案:

    像这样制作你的资源。

    res/布局/my_layout.xml

    普通屏幕尺寸的布局(“默认”)

    res/layout-large/my_layout.xml

    大屏幕尺寸的布局

    如果您不知道如何创建一个大布局文件夹,您所要做的就是在 Project Explorer 路径中创建一个同名的文件夹并将您的图层放在那里。 然后,Android 本身将自动放置在您在 Android Studio Live 文件夹中的每一层的单独文件夹中。 例如,activity_main 文件夹由两层组成,如下所示

    【讨论】:

    • 感谢萨曼的回答。基本上,我上面摆出的所有 3 个屏幕都是来自平板电脑,它们在屏幕尺寸方面基本上没有太大差异,但输出看起来仍然有很大不同。因此,按照您建议的方法,我必须为每个可能的屏幕尺寸创建一个布局文件?所以这意味着我将有超过 20 个布局文件用于一个布局。鉴于我的应用程序中有大约 30 个不同的片段,这将导致 30*20 = 600 个布局文件,这是非常多的。
    【解决方案4】:

    首先简化视图树。使用虚拟布局而不是嵌套。您可以使用Guideline、Flow、app:layout_constraintDimensionRatio 属性或带权重的链来让 ConstraintLayout 的子级使用所需的比例。 这个article 可以帮到你

    另一种方法是创建不同的layout with qualifiers。最好使用单一布局进行更好的设计管理。

    【讨论】:

    • 感谢德国人的回答。您对限定词的确切含义是什么?在您发送给我的链接中,您可以使用它们来区分纵向和横向模式。在我的应用程序中,我只使用横向模式。在你的文章中进一步说我不应该使用硬编码的尺寸。我通过使用这个库中的可缩放大小单位来解决这个问题github.com/intuit/sdp
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多