实例入下图:我想通过如下代码实现第二个edittext与第一个edittext左边缘对齐
![]()
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:layout_alignLeft="@id/editText1" /> </LinearLayout>有以下三种方法解决:第一种方法设置两个TextView的宽度为固定值,且相等。LinearLayout是一种线性排列的布局,布局中的控件从左到右(或者是从上到下)依次排列。wrap_content依据其内容分配宽度,“用户名”和“密码”由于内容长度不同,导致对应的两个TextView不等宽,而其后紧跟EditText,这就导致了不对齐。第2种方法,把LinearLayout中的组件宽度设置为0dp,然后设置其android:layout_weight,比如TextView设置为0.2,EditText设置为0.8,这两个控件就会按比例分配整个LinearLayout的宽度。第二个LinearLayout也同样设置,就可以保证EditText对齐。
第3种方法,使用RelativeLayout,通过android:layout_alignLeft="@id/anotherViewId"设置该View的左边和指定View的左边对齐。 但是你这种布局情况很简单,我认为用方法1和2比较方便。
多个TextView和EditView注册界面左对齐的方法
最新推荐文章于 2024-04-03 21:01:27 发布
3679

被折叠的 条评论
为什么被折叠?



