search

Home  >  Q&A  >  body text

android:怎么实现一个控件与另一个指定控件左对齐

如图,我想通过如下代码实现第二个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>
天蓬老师天蓬老师2772 days ago862

reply all(12)I'll reply

  • 高洛峰

    高洛峰2017-04-17 14:50:52

    For your situation, the simplest way is to set the width of the two TextViews to a fixed value and equal them.

    LinearLayout is a linear layout. The controls in the layout are arranged from left to right (or top to bottom). wrap_contentThe width is allocated according to its content. Due to the different content lengths of "Username" and "Password", the corresponding two TextViews are unequal in width, followed by EditText, which leads to misalignment.

    The second method is to set the width of the component in LinearLayout to 0dp, and then set it android:layout_weight, for example, if TextView is set to 0.2 and EditText is set to 0.8, the two controls will allocate the width of the entire LinearLayout proportionally. The second LinearLayout is also set in the same way to ensure that the EditText is aligned.

    The third method is to use RelativeLayout to set the left side of the View to align with the left side of the specified View through android:layout_alignLeft="@id/anotherViewId".
    But your layout situation is very simple. I think methods 1 and 2 are more convenient.

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 14:50:52

    Not much to say about setting weights, setting fixed values, relative layout, etc.!

    It is recommended to use GridLayout
    Using GridLayout layout can perfectly solve your problem and reduce the application of LinearLayout
    Reference for specific usage: A brief discussion of GridLayout layout in android 4.0 development
    Note: Systems above 4.0 can be used directly, systems below 4.0 need to import the v7 package

    Someone in the comments mentioned using TableLayout, but it needs to be used in combination with TableRow for layout, which is not recommended.
    TableLayout related usage reference: android layout------TableLayout (table layout) detailed explanation

    Difference reference between GridLayout and TabLayout: GridLayout and TableLayout layout

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:50:52

    Look at the relative layout!

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 14:50:52

    Give textview the same fixed width

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 14:50:52

    LinearLayout does not support the android:layout_align* attributes. If you want to use this series of attributes, you can change it to RelativeLayout.
    But depending on your needs, you can actually use TableLayout (of course the official said it is outdated and is not recommended), or like the answer above, set layout_width to 0dp and set the width of the component through weight. This is also The most common way for everyone to write this kind of page.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 14:50:52

    There is a super simple solution to align the password... Add a full-width space between the two characters of the password

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 14:50:52

    Such a simple question. . . maginLeft="100dp"

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:50:52

    You can also use tablelayout

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 14:50:52

    1. The simplest way is to change it to a relative layout and just drag it to align.
    2. Lay out linear layouts for the two input boxes, and align the two linear layouts.

    reply
    0
  • PHPz

    PHPz2017-04-17 14:50:52

    Give you a suggestion, in LinearLayout, use the weight attribute of View to control the duty cycle of the subspace
    For example, set the weight of Text to 1 and the weight of EditText to 2, then in the horizontal layout, the two The controls have a 1:2 relationship, so naturally the upper and lower EditTexts are aligned

    reply
    0
  • Cancelreply