android - 在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>
解决方案
您需要使用约束布局来获取百分比的宽度/高度。
这两行代码将为您完成这项工作。
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>
推荐阅读
- javascript - 使用自动完成 jquery 和 Odoo rpc 调用的输入不显示建议
- angularjs - How to use track by in ng-repeat having array in ng-model
- java - 如何将位图图像添加到int数组
- android - singleLine true for Textview,But multiple lines comes in one line..,Need to show 1st line
- c# - Passing parameter to asmx web-service in Postman
- python-3.x - Keras One 热编码内存管理 - 最好的可能出路
- javascript - 如何解析具有多个值的json数据?
- asp.net-mvc - 通过 BillQuery 从 qbxml 中的 QuickBooks 获取账单?
- optimization - 为什么“下载”文件内容的时间很长
- apache - ImportError:在 httpd 上使用 mod_wsgi 部署烧瓶应用程序时没有名为 pyodbc 的模块