简体   繁体   English

Android对齐textview和edittext

[英]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文件中。我正在尝试对齐contents.ie,第一个textview后跟edittext,然后在下一行中进行操作。但是现在紧接着文本会相互强加如何解决此问题。

 <?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 . 另一种方法,对我来说效果很好,在你的xml文件中,添加一个父<LinearLayout>来指定android:orientation="horizontal"来包含你的TextViewEditText

This parent LinearLayout has to be inside your very first LinearLayout that has to specify a android:orientation="vertical" : 这个父LinearLayout必须你的第一个必须指定android:orientation="vertical" LinearLayout中:

<!-- 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. 我有同样的问题试图目标android 4.X. I solved it by using android:gravity="center_vertical" 我通过使用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. 您为多个视图使用相同的ID,然后多个视图使用相同的id使用layout_below ,这就是为什么有一些在彼此之上。 Make them unique ids. 让他们成为独特的ids。 Also the id you are using for some align_left arnt even there, like editText1 for example. 还有你用于某些align_left的id甚至在那里,比如editText1

Short answer: use unique ids for each view and check what your referencing for alignments and positioning etc. 简短回答:为每个视图使用唯一ID,并检查对齐和定位等的引用。

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. 你的下一个问题是你不应该尝试像这样分层你的观点,有一些叫做android:hint="hint text"这就是我认为你想要的。 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. 我所做的是将android:textAlignment="center"到XML中的textView。 the edit text is centered by default. 编辑文本默认居中。

Both the TextView and the EditText are in a horizontal LinearLayout . TextViewEditText都在水平LinearLayout

将此属性添加到EditText:

android:layout_below="@+id/hostname"

The Best way is remove all the textview and set andoid:hint to edit text . 最好的方法是删除所有textview并设置andoid:hint编辑文本。

<?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>

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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