【问题标题】:Layout width equal height布局宽度等于高度
【发布时间】:2020-09-03 19:01:45
【问题描述】:

有没有一种方法可以让高度取决于我的 xml 中的宽度?

我有一些线性布局的按钮。按钮的宽度取决于设备,因为它们水平填充屏幕。我想要方形按钮,这是我的问题。 有没有人可以帮帮我?

<Button
            android:id="@+id/b_0xa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="false"
            android:text="@string/b_0xa" />

【问题讨论】:

  • 您最初可以通过 xml 将默认尺寸设置为“wrap_content”,当您的 Activity 开始时获取按钮的显示高度和宽度,并从那里以编程方式根据设备尺寸更改尺寸

标签: android android-layout button


【解决方案1】:

要使view的height等于view的width,可以使用ConstraintLayout

 app:layout_constraintDimensionRatio="1:1"

1 之前的: 是宽度

: 之后的1 是高度

请尝试以下代码:

<?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"
    tools:context=".MainActivity">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Button"/>

</androidx.constraintlayout.widget.ConstraintLayout>

上面代码的输出是:

【讨论】:

    【解决方案2】:

    您可以在 Java 代码中以编程方式轻松完成。

    动态获取按钮的宽度[使用int x = mButton.getWidth()],然后使用返回的值设置高度[mButton.setHeight(x)]。

    附注: 建议使用 LayoutParams 来设置任何 View 的高度和宽度。因此,您应该使用 setLayoutParams(),而不是使用 setHeight()。

    mButton.setLayoutParams(new LinearLayout.LayoutParams(x, x));

    【讨论】:

      【解决方案3】:

      您应该通过为android:layout_widthandroid:layout_height 分配固定值来更改赋予它们的值

      <Button
                  android:id="@+id/b_0xa"
                  android:layout_width="50dp"
                  android:layout_height="50dp"
                  android:layout_weight="1"
                  android:clickable="false"
                  android:text="@string/b_0xa" />
      

      为它们分配固定值可确保无论屏幕大小如何,您的按钮都保持不变,因为使用的单位是 dp。确保您分配的值相同,因为您需要一个正方形。

      【讨论】:

        【解决方案4】:

        在这种情况下,我建议您使用 layout_weight 作为您的高度属性,然后它会为按钮分配一个比例权重,该按钮将在不同的屏幕尺寸上进行缩放:

        <Button
                android:id="@+id/b_0xa"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight=".4"
                android:clickable="false"
                android:text="@string/b_0xa" />
        

        为了使这种方法起作用,您必须对布局中其他视图的高度应用权重。总重量必须等于 1,因此您必须调整布局和重量以实现最佳设计。

        【讨论】:

          【解决方案5】:

          正如我在here 解释的那样,如果您想 100% 使用 XML,您可以使用 PercentRelativeLayout 并执行以下操作:

          <android.support.percent.PercentRelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
          
              <Button
                  app:layout_aspectRatio="100%"
                  app:layout_widthPercent="100%" />
          </android.support.percent.PercentRelativeLayout>
          

          确保在您的应用 gradle 文件中包含该库:

          compile 'com.android.support:percent:XX.X.X'
          

          【讨论】:

            【解决方案6】:

            您可以使用约束布局来实现这一点。我正在使用宽高比属性和按钮宽度的绝对值。当我的 Button 根据它的宽度达到约束时,它会相应地设置我的高度。

              <?xml version="1.0" encoding="utf-8"?>
              <androidx.constraintlayout.widget.ConstraintLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                xmlns:app="http://schemas.android.com/apk/res-auto">
            
                <Button
                    android:id="@+id/b_0xa"
                    android:clickable="false"
                    android:text="@string/app_name"
                    android:layout_width="300dp"
                    android:layout_height="0dp"
                    android:src="@android:drawable/dark_header"
                    app:layout_constraintDimensionRatio="h,16:9"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
            
            </androidx.constraintlayout.widget.ConstraintLayout>
            

            【讨论】:

              【解决方案7】:
              <Button
                android:id="@+id/random"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:text="text view" 
              />
              

              确保您提供此代码或手动更改高度和宽度

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2011-07-23
                • 2019-04-17
                • 1970-01-01
                • 1970-01-01
                • 2014-11-11
                • 2013-02-16
                相关资源
                最近更新 更多