简体   繁体   中英

Android align textview and edittext

In the following xml file.i am trying to align the contents.ie, first textview followed by the edittext and so on in the next line.But tight now the text is imposed one on another how to resolve this issue.

 <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" >


      <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="0.70" >



          <TextView
              android:id="@+id/hostname"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/ip"
              android:layout_alignParentLeft="true"
              android:text="hostname" />


          <EditText
              android:id="@+id/ip"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_alignParentTop="true"
              android:layout_marginLeft="39dp"
              android:ems="10"
              android:inputType="textEmailAddress" >

          </EditText>


   <TextView
              android:id="@+id/username"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/ip"
              android:layout_alignBottom="@+id/ip"
              android:layout_alignParentLeft="true"
              android:text="Hostname" />
          <EditText
              android:id="@+id/username"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignLeft="@+id/editText1"
              android:layout_below="@+id/ip"
              android:layout_marginLeft="45dp"
              android:layout_marginTop="42dp"
              android:ems="10" />

           <TextView
              android:id="@+id/password"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/hostname"
              android:layout_alignBottom="@+id/username"
              android:layout_alignParentLeft="true"
              android:text="Password" />
          <EditText
              android:id="@+id/password"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_below="@+id/password"
              android:layout_centerHorizontal="true"
              android:layout_marginTop="47dp"
              android:ems="10"
              android:inputType="textPassword" />


      </RelativeLayout>

  </LinearLayout>

Another way to do it, that worked well for me, is, in your xml file, to add a parent <LinearLayout> that specifies android:orientation="horizontal" to encompass your TextView and EditText .

This parent LinearLayout has to be inside your very first LinearLayout that has to specify a android:orientation="vertical" :

<!-- Root Layout -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" <!-- Vertical orientation -->
    android:paddingLeft="15sp"
    android:paddingRight="15sp"
    android:paddingTop="15sp"
    android:paddingBottom="15sp" >


    <!-- First Row -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" <!-- Horizontal orientation for the row -->
        android:paddingTop="15sp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Poids :"
            android:textSize="15sp"
            android:textStyle="bold" />
        <EditText
            android:id="@+id/btn_poids"
            android:inputType="numberDecimal"
            android:hint="Entrez votre poids"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>


    <!-- Second Row -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" <!-- Horizontal orientation for the row -->
        android:paddingTop="15sp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Taille :"
            android:textSize="15sp"
            android:textStyle="bold" />
        <EditText
            android:id="@+id/btn_poids"
            android:inputType="numberDecimal"
            android:hint="Entrez votre taille"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>


</LinearLayout>

Here's the result (the warning signs are because of the hardcoded strings for the hints and text...) :

结果如上

I had the same issue trying to target android 4.X. I solved it by using android:gravity="center_vertical"

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:ems="10"
            android:id="@+id/itemNameLabel"
            android:text="@string/entry_item_label"
            android:textSize="@dimen/baseTextSize"
            android:layout_weight=".15"
            android:gravity="center_vertical" />

        <EditText
            android:layout_width="0dp"
            android:layout_weight=".85"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/itemName"
            android:textSize="@dimen/baseTextSize"
            android:hint="Food, Video Game, etc..." />
    </LinearLayout>

Your using the same id for multiple views, and then multiple views are using layout_below using the same id, that's why there are some that are on top of each other. Make them unique ids. Also the id you are using for some align_left arnt even there, like editText1 for example.

Short answer: use unique ids for each view and check what your referencing for alignments and positioning etc.

Your next problem is that you shouldn't try and layer your views like this, there is something called android:hint="hint text" this is what I think you want. The text will hide once the user starts to input text, but other then that its like there is a text view on top of it...

None of these solved my problem. What I do is I add android:textAlignment="center" to the textView in XML. the edit text is centered by default.

Both the TextView and the EditText are in a horizontal LinearLayout .

将此属性添加到EditText:

android:layout_below="@+id/hostname"

The Best way is remove all the textview and set andoid:hint to edit text .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/ip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:hint="HostName"
        android:inputType="textEmailAddress" >
    </EditText>

    <EditText
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="HostName" />

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword" />

</LinearLayout>

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM