2012-12-28 43 views
0

我有這種線性佈局和線性佈局內我有幾個文本視圖。 現在我想要做的是在左側顯示信息文本,然後顯示它旁邊的值,但值應該在最長的信息文本後對齊。Android UI如何對齊textviews?

我看不到我可以如何使用線性佈局內的另一個線性佈局。

  <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="5dp" 
      android:orientation="vertical" > 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="Room:" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="{value}" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="Date:" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="{value}" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="Time:" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="{value}" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="Location" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="{value}" 
       android:textSize="12dp" /> 
     </LinearLayout> 

像一張html表格。一個與信息文本,然後是下一個值。 帶有文本值的文本視圖應該在最長的信息文本之後對齊。

的Html相當於

<table> 
<tr> 
<td>Room:</td> 
<td>{value}</td> 
</tr> 
<tr> 
<td>Date:</td> 
<td>{value}</td> 
</tr> 
... 
</table> 

,纔有可能對準這除了投入固定DP寬度是多少?

回答

1

試試這個代碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:background="#0099cc" 
    tools:context=".FullscreenActivity" > 

    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="Room:" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="{value}" 
       android:textSize="12dp" /> 
     </TableRow> 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="Date:" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="{value}" 
       android:textSize="12dp" /> 
     </TableRow> 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="Time:" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="{value}" 
       android:textSize="12dp" /> 
     </TableRow> 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="Location" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="{value}" 
       android:textSize="12dp" /> 
     </TableRow> 
    </TableLayout> 

</LinearLayout> 
2

這樣的事情,

Room: Value 
Date: Value 

你可以用這樣的佈局,

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="5dp" 
      android:orientation="vertical" > 

    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="5dp" 
      android:orientation="horizontal" > 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="Room:" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="{value}" 
       android:textSize="12dp" /> 
     </LinearLayout> 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="5dp" 
      android:orientation="horizontal" > 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="Date:" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="{value}" 
       android:textSize="12dp" /> 
     </LinearLayout> 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="5dp" 
      android:orientation="horizontal" > 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="Time:" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="{value}" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="Location" 
       android:textSize="12dp" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff00ff" 
       android:text="{value}" 
       android:textSize="12dp" /> 
     </LinearLayout> 
     </LinearLayout> 

如果你想創建一個像strucutre一個表,那麼你應該看看Table Layout。你會發現一個很好的例子here

0

U可以放在一個LinearLayout中的textViews像下面

 <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="5dp" 
       android:layout_marginLeft="5dp" 
       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:background="#ff00ff" 
        android:text="Room:" 
        android:textSize="12dp" /> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="#ff00ff" 
        android:text="{value}" 
        android:textSize="12dp" /> 
    </LinearLayout> 

    // add the rest of textviews similarly as above 
</LinearLayout>