首页 > 解决方案 > 在xml中分组项目,按百分比设置宽度

问题描述

我正在尝试创建登录布局,但我希望将 xml 中的所有项目分组,以便它们可以像一个项目一样居中,并且我想将 editTexts 设置为百分比宽度而不是硬编码它,这种方式它可以更好地适应不同尺寸的屏幕。

我尝试使用 layout_weight 但由于某种原因它影响了editTexts的高度而不是宽度。

这是我的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#dedede"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:src="@drawable/logo_big" />

    <EditText
        android:id="@+id/scardET"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:autofillHints=""
        android:background="@drawable/selector_edittext"
        android:hint="Carnet"
        android:inputType="text"
        android:text="" />

    <EditText
        android:id="@+id/passwordET"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:autofillHints=""
        android:background="@drawable/selector_edittext"
        android:hint="Contraseña"
        android:inputType="textPassword"
        android:text="" />

    <Button
        android:id="@+id/loginbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/login_button"
        android:minHeight="0dp"
        android:text="entrar"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

</LinearLayout>

标签: androidandroid-layout

解决方案


您需要使用约束布局来获取百分比的宽度/高度。

这两行代码将为您完成这项工作。

app:layout_constraintWidth_percent="0.8"
app:layout_constrainedWidth="true"

允许您以百分比设置宽度的第一行。第二行将确保您的视图始终遵守百分比宽度。

下面是您的代码更正以处理百分比宽度。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#dedede"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:src="@drawable/logo_big" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/scardET"
            android:layout_width="0dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:autofillHints=""
            android:background="@drawable/selector_edittext"
            android:hint="Carnet"
            android:inputType="text"
            app:layout_constraintWidth_percent="0.7"
            app:layout_constrainedWidth="true"
            android:text="" />

        <EditText
            android:id="@+id/passwordET"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/scardET"
            app:layout_constraintWidth_percent="0.8"
            app:layout_constrainedWidth="true"
            android:autofillHints=""
            android:background="@drawable/selector_edittext"
            android:hint="Contraseña"
            android:inputType="textPassword"
            android:text="" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <Button
        android:id="@+id/loginbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/login_button"
        android:minHeight="0dp"
        android:text="entrar"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

</LinearLayout>

推荐阅读