2011-12-09 7 views
26
<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Street" 
      android:layout_gravity="left"/> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="456546546" 
      android:layout_gravity="right" /> 

    </LinearLayout> 

</LinearLayout> 

2つの列を持つレイアウトを作成しようとしていますが、左側に1つのテキストビューと右側に1つのテキストビューがあります。しかし、テキストビューはまだすべて左側にあります。Android:直線レイアウトに2つの列を作成する

答えて

74

android:layout_weight属性を使用してください。次に例を示します。

<LinearLayout 
    android:id="@+id/linearLayout2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="Street" 
     android:layout_gravity="left" 
     android:background="#88FF0000"/> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="456546546" 
     android:layout_gravity="right" 
     android:background="#8800FF00"/> 

</LinearLayout> 

enter image description here

+1

はい、これは同様に動作します。左のテキストと右のテキストではなく、完全な列が必要な場合は、おそらくより効果的です。 –

1

これを行うときにテーブルレイアウトを試してください。グラフィカルなレイアウトのドラッグテーブルのレイアウトでは、項目をセルに入れます。

4

うん、これは紛らわしいです。 LinearLayoutの幅はfill_parentに設定されていますが、それでも必要な最小幅しか取れません。あなたはfill_parentし、右側にその重力する第二のTextViewを設定する必要があります。

<LinearLayout 
    android:id="@+id/linearLayout2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Street"/> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="456546546" 
     android:gravity="right" /> 

</LinearLayout> 
+0

これは、 "weight"を指定するよりも少しうまく最適化します。 – csi

3

uは、各列に複数の行を持つようにしたい場合はuが私はそれはあなたに役立つことを願っていますTable Layout

3

を使用することができます。

このコードを試してみてください。..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 


    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:background="#00FF00" 
     android:paddingRight="90dp" 
     android:orientation="vertical" > 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/b1" 
      android:text="Button 1"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#FF00FF" 
     android:orientation="vertical" >  

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/b2" 
      android:text="Button 2"/> 

    </LinearLayout> 

</LinearLayout> 
2

そして、あなたはボタンの間のギャップを必要とする場合:

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

    <Button 

     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.95" 
     android:text="Via SMS" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.05" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.95" 
     android:text="Diaporama" /> 
</LinearLayout> 
0

あなただけの列のための2つの垂直LinearLayoutsを有する潜在的な問題は何も行う保証していないということです行の高さが可変であればラインアップします。 TableLayoutはこれに最も適しています。また、列が縮小または拡大して使用可能な領域を埋める方法を制御できます。

@ santhosh-shettigarが投稿されて以来、リンクが変更されています。
ガイド:https://developer.android.com/guide/topics/ui/layout/grid.html
参考:http://developer.android.com/reference/android/widget/TableLayout.html

関連する問題