2017-03-07 117 views
0

我有一个TextView。我想做所有的TextView(No.123 ... & Sea View ...)左对齐。我知道android:gravity="left"android:layout_gravity="left"但它不适合我的问题。如何对齐android中的所有TextView?

这是我的xml代码;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:card_view="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

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

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Address " /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="No.123, Blah Street, Blah Township, Blah Country" /> 

     </LinearLayout> 

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

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Type "/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Sea View, Graden View, Local" /> 

     </LinearLayout> 
</LinearLayout> 

如何使用align left text view?如下图所示。

enter image description here

输出结果;

enter image description here

+0

什么是您的输出?你能分享屏幕截图吗?并请更新代码片段 –

+1

中的LinearLayout的父布局,以某种方式显示您想要移动的内容以及以何种方式。不清楚。 –

+0

见下文http://stackoverflow.com/a/42641608/3513479 没有必要改变你的代码只需把一条线 –

回答

2

使用相对布局来实现你想要的,

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/address" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Address " /> 

     <TextView android:id="@+id/type" 
      android:layout_below="@+id/address" 
      android:layout_alignRight="@+id/address" 
      android:layout_width="match_parent" 
      android:gravity="left" 
      android:layout_height="wrap_content" 
      android:text="Type "/> 

     <TextView 
      android:layout_toRightOf="@+id/address" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="No.123, Blah Street, Blah Township, Blah Country" /> 

     <TextView 
      android:layout_alignBottom="@+id/type" 
      android:layout_toRightOf="@+id/type" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Sea View, Graden View, Local" /> 
    </RelativeLayout> 

enter image description here

+0

哦!我忘记使用RelativeLayout。谢谢。 – WPG

0

随着layout_weight和weighSum的帮助下,你可以做到这一点,下面的代码片段可以帮助你

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

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.2" 
      android:text="Address" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_weight="1.8" 
      android:layout_height="wrap_content" 
      android:text="No.123, Blah Street, Blah Township, Blah Country" /> 

    </LinearLayout> 

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

     <TextView 
      android:layout_width="0dp" 
      android:layout_weight="0.2" 
      android:layout_height="wrap_content" 
      android:text="Type"/> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1.8" 
      android:text="Sea View, Graden View, Local" /> 

    </LinearLayout> 

</LinearLayout> 

有关的LinearLayout和它的属性更多的了解,你可以参考 https://developer.android.com/reference/android/widget/LinearLayout.html

0

使用layout_weight和LinearLayout中的weightSum财产,这将使你的布局正确对齐和响应了。

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

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

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Address" /> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="4" 
     android:layout_height="wrap_content" 
     android:text="No.123, Blah Street, Blah Township, Blah Country" /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="5" 
    android:layout_marginTop="16dp" 
    android:orientation="horizontal"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="Type"/> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="4" 
     android:text="Sea View, Graden View, Local" /> 

</LinearLayout> 

1

没有做更多的在你的代码只需添加一行 要将地址&类型

<TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Address" /> 

,而不是

<TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:ems="5" 
     android:text="Address" /> 

删除安卓layout_weight = “1”

可以增加减少EMS值按您的要求

现在你说的话是EMS

EMS指到XML布局文件到这是我的固定宽度基于文本大小:

如果您设置了android:ems =“5”,那么它会消耗5个字符

为您设计的空间。 这意味着间接您可以设置您的布局或TextText的宽度。

享受编码;)