簡體   English   中英

布局寬等高

[英]Layout width equal height

有沒有一種方法可以讓高度取決於我的 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" />

要使視圖的height等於視圖的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>

上面代碼的輸出是:

在此處輸入圖片說明

您可以在 Java 代碼中以編程方式輕松完成。

動態獲取按鈕的寬度[使用int x = mButton.getWidth() ],然后使用返回的值設置高度 [ mButton.setHeight(x) ]。

PS:建議使用LayoutParams來設置任意View的高度和寬度。 因此,不應使用 setHeight(),而應使用 setLayoutParams()。

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

您應該通過為它們分配固定值來更改賦予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。 確保您分配的值相同,因為您需要一個正方形。

在這種情況下,我建議您將 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,因此您必須調整布局和重量以實現最佳設計。

正如我在此處解釋的如果您想 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'

您可以使用約束布局來實現這一點。 我正在使用縱橫比屬性和按鈕寬度的絕對值。 當我的 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>
<Button
  android:id="@+id/random"
  android:layout_width="60dp"
  android:layout_height="60dp"
  android:layout_weight="1"
  android:text="text view" 
/>

確保您提供此代碼或手動更改高度和寬度

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM