2016-04-05 4 views
1

私は線形レイアウト内に4つの要素を持っています。LinearLayout内の要素を半分のスクリーンスペースを使って整列する

classALabel & classBLabelが画面の半分の幅を横に並べて並べられるように並べ替える必要があります。

そしてclassA & classBまた、右隣の画面の半分半分の幅を取って並んで互いの側ではなくclassALabel & classBLabel以下の整列します。

以下は私が今まで試したことですが、これは私が望むように動作しません。 4つの要素すべてを並べて、画面空間の25%ずつ並べて表示しています。

<LinearLayout 
    android:id="@+id/ClassALayout" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/tile_height" 
    android:layout_below="@+id/ClassLayout" 
    android:layout_marginBottom="8dp" 
    android:layout_marginTop="8dp" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/classALabel" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="@string/classALabel" /> 

    <TextView 
     android:id="@+id/classA" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/classALabel" 
     android:layout_marginTop="@dimen/dimen_2" 
     android:layout_alignParentLeft="true" 
     android:layout_weight="1" 
/> 

    <TextView 
     android:id="@+id/classBLabel" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/classA" 
     android:layout_weight="1" 
     android:text="@string/classBLabel" /> 

    <TextView 
     android:id="@+id/classB" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/classBLabel" 
     android:layout_marginTop="@dimen/dimen_2" 
     android:layout_weight="1" 
/> 
</LinearLayout> 

答えて

1

これは、水平方向のLinearLayoutを1つだけ作成したためです。このレイアウトの中に配置するすべてのビューは同じ行になります。

親のレイアウトを作成すること、縦書きの線形レイアウトを作成すること、そしてこのレイアウトの内側に水平方向の2つの別々の線形レイアウトを作成し、それぞれに2つのテキストビューを配置するだけです。

2

私はあなたのための最善の解決策は、この

<TableLayout 
android:id="@+id/ClassALayout" 
android:layout_width="match_parent" 
android:layout_height="@dimen/tile_height" 
android:layout_below="@+id/ClassLayout" 
android:layout_marginBottom="8dp" 
android:layout_marginTop="8dp" 
> 
<TableRow 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"> 

<TextView 
    android:id="@+id/classALabel" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="@string/classALabel" /> 

<TextView 
    android:id="@+id/classA" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="@dimen/dimen_2" 
    android:layout_weight="1" 
    /> 
</TableRow> 
<TableRow 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:layout_weight="1"> 
<TextView 
    android:id="@+id/classBLabel" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="@string/classBLabel" /> 

<TextView 
    android:id="@+id/classB" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="@dimen/dimen_2" 
    android:layout_weight="1" 
    /> 
</TableRow> 

またはあなたが本当にandroid:orientation="horizontal"と2つの子リニアレイアウトでandroid:orientation="vertical"と親LinearLayoutを使用する必要がなく、LinearLayoutを使用する場合だと思いますあなたが持つ相対的なサイズ(つまり、android:layout_weightの使用を意味する)を持つ子レイアウトが多いほど、システムへの課金が増えることに注意してください。

それとも、相対的なレイアウトを使用することができますが、あなたは水平方向の単一のリニアレイアウトにあなたの意見のすべてを入れているので、何が起こっているandroid:layout_weight

1

を使用することはできません。重み= 1のビューが4つあるので、画面は4等分に分割されます。

以下のコードは、あなたが望むものを達成するのに役立ちます。

Classalayout線形レイアウトは、2つの子線形レイアウト(LabelContainer線形レイアウトとClassContainer線形レイアウト)をさらに含む親レイアウトです。 ClassContainerレイアウトは、LabelContainerレイアウトの下にあります。これは、親レイアウトに垂直方向があるためです。子レイアウトには2つのtextviewが含まれています。 ラベルtextviewsはクラスtextviewsの上にあり、すべてが画面スペースの50%を占めます。

<LinearLayout     
android:id="@+id/ClassALayout" 
android:layout_width="match_parent" 
android:layout_height="@dimen/tile_height" 
android:layout_below="@+id/ClassLayout" 
android:layout_marginBottom="8dp" 
android:layout_marginTop="8dp" 
android:orientation="vertical"> 

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

<TextView 
    android:id="@+id/classALabel" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="@string/classALabel" /> 

<TextView 
    android:id="@+id/classBLabel" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="@string/classBLabel" /> 


</LinearLayout> 

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

<TextView 
    android:id="@+id/classA" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="@dimen/dimen_2" 
    android:layout_weight="1"/> 

<TextView 
    android:id="@+id/classB" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="@dimen/dimen_2" 
    android:layout_weight="1"/> 

</LinearLayout> 
</LinearLayout> 
0

その迅速かつ汚いソリューションが、それはあなたが望むあなたを与えるだろう。ここでは

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

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </LinearLayout> 

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

     <TextView 
      android:id="@+id/classALabel" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="classALabel" /> 

     <TextView 
      android:id="@+id/classBLabel" 
      android:layout_width="match_parent" 
      android:layout_weight="0.5" 
      android:layout_height="wrap_content" 
      android:text="ClassBLabel" /> 
    </LinearLayout> 
</LinearLayout> 

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

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </LinearLayout> 

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

     <TextView 
      android:id="@+id/classA" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:text="ClassA" 
      android:layout_weight="1" 
      /> 

     <TextView 
      android:id="@+id/classB" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:layout_weight="1" 
      android:text="ClassB" 
      /> 

    </LinearLayout> 

</LinearLayout> 

が出力されます。

enter image description here

スクリーンサイズの半分。

関連する問題