布局宽度等于高度

10

有没有一种方法可以使我的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" />

1
您可以最初通过xml将默认尺寸设置为“wrap_content”,在Activity启动时获取按钮的显示高度和宽度,并根据设备尺寸在程序中更改尺寸。 - Jade Byfield
7个回答

4
使用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>

以上代码的输出结果为:

enter image description here


0

您应该更改给定的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。确保分配的值相同,因为您想要一个正方形。


0
在这种情况下,我建议您使用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,因此您将不得不在布局和权重之间进行调整,以实现最佳设计。

0

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

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

P.S. : 建议使用LayoutParams来设置任何视图的高度和宽度。因此,您应该使用setLayoutParams()而不是setHeight()。

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


0

如我在这里所解释的,如果你想要完全使用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'

0
<Button
  android:id="@+id/random"
  android:layout_width="60dp"
  android:layout_height="60dp"
  android:layout_weight="1"
  android:text="text view" 
/>

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


0
你可以使用 Constraint Layout 实现这个效果。我正在使用纵横比属性和按钮宽度的绝对值。当我的按钮根据它的宽度达到限制时,它会相应地设置高度。
  <?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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接