2017-04-25 38 views
0

我的申請(https://i.stack.imgur.com/xFozp.png對齊2 textviews水平機器人

正如我水平對齊textviews在圖像中看到的屏幕。當產品名稱更長時,名稱將在文本視圖中以2行顯示。 但是,當textview中的行數增加時,兩個textview的起始行不是水平對齊的。 我希望textviews的起始行水平對齊。 目前我正在使用LINEAR佈局。我應該改變佈局嗎?任何人都可以編寫代碼。 因此,即使2個文本瀏覽互相之間(如圖中所示),他們的開始(第一行)應該在同一水平線。

我的代碼如下 -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="10dp" 
    android:background="@drawable/b22" 
    android:gravity="center|left" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
     android:text="TextView" 
     android:textColor="@android:color/black" 
     android:textSize="15dp" 
     android:maxLines="3" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right|center" 
     android:text="TextView" 
     android:textColor="@android:color/black" 
     android:maxLines="3" /> 

</LinearLayout> 
+1

發表您的佈局....代碼 – rafsanahmad007

+0

嗨,添加了我的佈局代碼。 – user3444096

回答

0

1.刪除屬性android:gravity="center|left"

2.刪除屬性android:layout_gravity="right|center"textView2並改變其爲android:layout_width="match_parent"寬度。

試試這個:

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

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
     android:text="Product Name -" 
     android:textColor="@android:color/black" 
     android:textSize="15dp" 
     android:maxLines="3" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="This is Liberty Agile Pension Preserver Product" 
     android:textColor="@android:color/black" 
     android:maxLines="3" /> 

</LinearLayout> 

OUTPUT:

enter image description here

爲您設計,其最好使用一個單一的RelativeLayout,並把所有的TextViews的裏面。

下面是一個例子:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
     android:text="Product Name -" 
     android:textColor="@android:color/black" 
     android:textSize="15dp" 
     android:maxLines="3" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/textView1" 
     android:layout_alignTop="@id/textView1" 
     android:text="This is Liberty Agile Pension Preserver Product" 
     android:textColor="@android:color/black" 
     android:maxLines="3" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/textView2" 
     android:layout_marginTop="20dp" 
     android:layout_marginRight="10dp" 
     android:text="Number of Senarios Executed -" 
     android:textColor="@android:color/black" 
     android:textSize="15dp" 
     android:maxLines="3" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/textView3" 
     android:layout_alignTop="@id/textView3" 
     android:text="400" 
     android:textColor="@android:color/black" 
     android:maxLines="3" /> 

</RelativeLayout> 

OUTPUT:

enter image description here

希望這將有助於〜

+0

非常感謝!它的工作很棒。 – user3444096

+0

最受歡迎:) – FAT

0

考慮使用LinearLayout爲兩個TextViewsweightSum。在總和中給他們兩個分量。下面是一個例子,從LinearLayout

<LinearLayout 
    android:id="@+id/text_layout" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="15dp" 
    android:layout_marginLeft="15dp" 
    android:layout_marginTop="5dp" 
    android:weightSum="2" 
    android:background="@android:color/white"> 

    <TextView 
    android:id="@+id/tvProductNameLabel" 
    android:layout_width="0dp" 
    android:layout_weight="0.6" 
    android:layout_height="wrap_content" 
    android:text="Product Name" 
    android:gravity="right" 
    android:textSize="14sp" 
    android:layout_marginRight="10dp"/> 

    <TextView 
    android:id="@+id/tvProductName" 
    android:layout_width="0dp" 
    android:layout_weight="1.4" 
    android:textSize="14sp" 
    android:textColor="@android:color/black" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:layout_marginLeft="10dp" 
    android:layout_gravity="left"/> 
</LinearLayout>