【问题标题】:How to set the size of buttons in a chain for different layouts in Android如何为Android中的不同布局设置链中按钮的大小
【发布时间】:2021-10-06 13:08:33
【问题描述】:

我想在我的 Android ConstraintLayout 中添加 3 个按钮,以便它们水平跨越。我认为这可以通过使用链来完成。所以我用它们制作了一条链,然后扩展了它们的视图(通过标记它们 --> 右键单击​​ --> 组织 --> 水平扩展)。然而,这只是将它们扩展到一个特定的设备。切换到另一台设备时,按钮看起来不再正确。这是我的代码:

<?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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/rigthInnerConstraintLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/white"
        app:layout_constraintBottom_toBottomOf="@+id/outerConstraintLayout"
        app:layout_constraintEnd_toEndOf="@+id/outerConstraintLayout"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="@+id/outerConstraintLayout"

        app:layout_constraintTop_toTopOf="@+id/outerConstraintLayout"
        app:layout_constraintVertical_bias="1.0">

        <Button
            android:id="@+id/button_1"
            android:layout_width="425dp"
            android:layout_height="@dimen/_30sdp"
            android:text="Button 1"
            app:layout_constraintEnd_toStartOf="@+id/button_2"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/button_2"
            android:layout_width="425dp"
            android:layout_height="@dimen/_30sdp"
            android:text="Button 2"
            app:layout_constraintEnd_toStartOf="@+id/button_3"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/button_1"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/button_3"
            android:layout_width="426dp"
            android:layout_height="@dimen/_30sdp"
            android:text="Button_3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/button_2"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

对于我使用可缩放的高度,我使用可缩放的尺寸单位 (https://github.com/intuit/sdp),但对于按钮的宽度,dp 值由 Android Studio 分配(在展开之后)。我认为这就是问题所在。这些 dp 值是硬编码的。这意味着在更改设备时,值仍然相同,这会导致外观不正确。我的问题是如何将 3 个按钮排列成一个链(或其他),以便它们水平跨越任何设备的一整行?

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    这些 dp 值是硬编码的。这意味着在更改设备时值仍然相同,导致外观不正确 - 这是正确的。

    如何解决

    如果您将按钮上的android:layout_width="426dp" 替换为android:layout_width="0dp"
    您的按钮现在将取消约束,现在每个按钮都将占用屏幕宽度的33.3%

    您可以通过将app:layout_constraintWidth_percent="0.x" 添加到您的按钮来更改每个按钮的宽度(仅当您的视图也有android:layout_width="0dp" 时才会起作用),现在您的按钮将占用屏幕宽度的x%。

    示例

    对于 3 个按钮位置连续中心按钮将占据屏幕大小的 50%,您可以像这样使用它:

    <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">
    
     <androidx.constraintlayout.widget.ConstraintLayout
         android:id="@+id/rigthInnerConstraintLayout"
         android:layout_width="0dp"
         android:layout_height="0dp"
         android:background="@color/white"
         app:layout_constraintBottom_toBottomOf="@+id/outerConstraintLayout"
         app:layout_constraintEnd_toEndOf="@+id/outerConstraintLayout"
         app:layout_constraintHorizontal_bias="1.0"
         app:layout_constraintStart_toStartOf="@+id/outerConstraintLayout"
    
         app:layout_constraintTop_toTopOf="@+id/outerConstraintLayout"
         app:layout_constraintVertical_bias="1.0">
    
      <Button
          android:id="@+id/button_1"
          android:layout_width="0dp"
          android:layout_height="50dp"
          android:text="Button 1"
          app:layout_constraintEnd_toStartOf="@+id/button_2"
          app:layout_constraintHorizontal_bias="0.5"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent" />
    
      <Button
          android:id="@+id/button_2"
          android:layout_width="0dp"
          app:layout_constraintWidth_percent="0.5"
          android:layout_height="50dp"
          android:text="Button 2"
          app:layout_constraintEnd_toStartOf="@+id/button_3"
          app:layout_constraintHorizontal_bias="0.5"
          app:layout_constraintStart_toEndOf="@+id/button_1"
          app:layout_constraintTop_toTopOf="parent" />
    
      <Button
          android:id="@+id/button_3"
          android:layout_width="0dp"
          android:layout_height="50dp"
          android:text="Button_3"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.5"
          app:layout_constraintStart_toEndOf="@+id/button_2"
          app:layout_constraintTop_toTopOf="parent" />
     </androidx.constraintlayout.widget.ConstraintLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    它看起来像这样:

    如果没有app:layout_constraintWidth_percent="0.5",每个按钮都将占据相同的空间,并且看起来像这样:

    【讨论】:

    • 感谢 Tamir 的回答。基本上我想出了另一个解决这个问题的方法。我只是将线性布局插入到约束布局中,然后将 3 个按钮添加到线性布局中。但是您的解决方案也很好。我投了赞成票。
    • 您的解决方案会使您的应用程序变慢,constraintLayout 的重点是防止这种嵌套布局。当您没有嵌套布局时,您的应用程序(在大多数情况下)会运行得更快,请查看this 了解更多信息
    • 感谢您的评论塔米尔。我将坚持使用嵌套布局的解决方案(因为我已经为许多片段实现了它)。稍后我会检查应用程序是否反应缓慢。老实说,我无法想象这会产生重大影响。此外,我在许多其他场合都使用过这种结构,到目前为止我还没有遇到任何困难。但是,我稍后会检查您指出的运行时是否存在显着差异。
    • 无论如何我接受了你的回答,因为它解决了问题。谢谢你的努力。我真的很感激。
    【解决方案2】:

    设置所有按钮的按钮宽度0dp,看看神奇:)

    android:layout_width="0dp"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 2021-10-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2013-07-23
      • 1970-01-01
      相关资源
      最近更新 更多