2014-06-13 48 views
1

我正在研究android應用。在這裏我有2 TextView,我需要一個在另一個之下。 (i.e "Artist Name" should be exact below the "Title of song")如何在android中將文本對齊到另一個下方?

任何人都可以幫助我將TextView一個在另一個之下。 在此先感謝。

下面是我的代碼

<RelativeLayout xmlns: 
    android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/list_selector" 
    android:orientation="horizontal" 
    android:padding="5dip" > 

    <TableLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_centerHorizontal="true" > 

    <!-- Left side Thumbnail image --> 
    <TableRow android:id="@+id/thumbnail" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="3dip" 
    android:layout_alignParentLeft="true" 
    android:background="@drawable/image_bg" 
    android:layout_marginRight="5dip"> 

<ImageView 
    android:id="@+id/list_image" 
    android:layout_width="50dip" 
    android:layout_height="50dip" 
    android:src="@drawable/doc"/> 



<!-- Title Of Song--> 
<TextView 
android:id="@+id/title" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignTop="@+id/thumbnail" 
android:layout_toRightOf="@+id/thumbnail" 
android:text="love you the" 
android:textColor="#040404" 
android:typeface="sans" 
android:textSize="15dip" 
android:textStyle="bold"/> 

<!-- Artist Name --> 
    <TextView 
    android:id="@+id/artist" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/title" 
    android:textColor="#343434" 
    android:textSize="10sp" 
    android:layout_marginTop="1dip" 
    android:layout_toRightOf="@+id/thumbnail" 
    android:text="Mary" /> 

    <!-- Rightend Duration --> 
    <TextView 
android:id="@+id/duration" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentRight="true" 
android:layout_alignTop="@id/title" 
android:gravity="right" 
android:text="7:45" 
android:layout_marginRight="5dip" 
android:textSize="10dip" 
android:textColor="#10bcc9" 
android:textStyle="bold"/> 

    <!-- Rightend Arrow --> 
    <ImageView android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/arrows" 
android:layout_alignParentRight="true" 
android:layout_centerVertical="true"/> 
</TableRow> 
</TableLayout> 

    </RelativeLayout> 
+0

這裏的竅門是,安卓重力是真正的財產,使用它。 – Jay

+0

這是您的程序檢查鏈接的最佳示例。 http://stackoverflow.com/questions/14000755/android-table-layout-alignment – Jay

+0

嘗試使用LinearLayout或使用屬性'android:layout_alignBottom =「@ + id/title」' –

回答

3

只需使用this即可將您的物品與其他物品相關聯。

androidHacker是正確的,你可以在一個文本視圖將低於其他的像這樣:

android:layout_below="@+id/your_another_text_view"

類似的佈局是在本教程爲你正在尋找在這裏here

+0

@ robi庫馬爾托馬爾,它不工作:( – user3607124

+0

什麼不工作,你能解釋一下更多。@ user3607124 –

+0

仍然是文本是相同的(即不對齊下面的歌曲的標題) – user3607124

2

嘗試這樣的..

android:layout_below="@+id/your_another_text_view" 

UPDATE

你只需要使用相對佈局的專門爲TextView的。試試吧,讓我知道。

+0

@ AndroidHacker,它不工作 – user3607124

1
<TextView 
android:id="@+id/title" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignTop="@+id/thumbnail" 
android:layout_toRightOf="@+id/thumbnail" 
android:text="love you the" 
android:textColor="#040404" 
android:typeface="sans" 
android:textSize="15dip" 
android:textStyle="bold"/> 

<!-- Artist Name --> 
    <TextView 
    android:id="@+id/artist" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/title" 
    android:textColor="#343434" 
android:layout_alignLeft="@+id/title"// This line is new 
    android:textSize="10sp" 
    android:layout_marginTop="1dip" 

    android:text="Mary" /> 
+0

@ Wasim Ahmed,沒有幫助:( – user3607124

+0

你已經接受了一個答案..所以我認爲你不需要furthure任何答案:) –

2
// try this way,hope this will help you to solve your problem. 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/list_selector" 
    android:orientation="horizontal" 
    android:padding="5dip" > 

    <TableLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_centerHorizontal="true" > 

     <!-- Left side Thumbnail image --> 
     <TableRow android:id="@+id/thumbnail" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="3dp" 
      android:background="@drawable/image_bg" 
      android:layout_marginRight="5dp"> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 
       <ImageView 
        android:id="@+id/list_image" 
        android:layout_width="50dp" 
        android:layout_height="50dp" 
        android:layout_alignParentLeft="true" 
        android:src="@drawable/ic_launcher"/> 


       <TextView 
        android:id="@+id/title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@id/list_image" 
        android:text="love you the" 
        android:textColor="#040404" 
        android:typeface="sans" 
        android:textSize="15sp" 
        android:textStyle="bold"/> 

       <!-- Artist Name --> 
       <TextView 
        android:id="@+id/artist" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/title" 
        android:layout_toRightOf="@id/list_image" 
        android:textColor="#343434" 
        android:textSize="12sp" 
        android:layout_marginTop="1dp" 
        android:text="Mary" /> 

       <TextView 
        android:id="@+id/duration" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@id/title" 
        android:gravity="right" 
        android:text="7:45" 
        android:layout_marginRight="5dp" 
        android:textSize="12sp" 
        android:textColor="#10bcc9" 
        android:textStyle="bold"/> 

       <ImageView 
        android:id="@+id/arrow_image" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_launcher" 
        android:layout_alignParentRight="true" 
        android:layout_toRightOf="@id/duration" 
        android:layout_centerVertical="true"/> 

      </RelativeLayout> 
     </TableRow> 
    </TableLayout> 
</RelativeLayout> 
+0

@ Haresh,謝謝:)它幫助我 – user3607124

+0

@ Haresh,文本正在對齊,但整個內容正在對齊中心:( – user3607124

+0

嘗試從TableLayout中刪除此屬性android:layout_centerHorizo​​ntal =「true」。 –

0

嘗試使用的LinearLayout或使用屬性android:layout_alignBottom="@+id/title"

+0

@ Ramki Anba,雅我嘗試過,但它不工作:( – user3607124

相關問題