2016-11-23 6 views
0

3つのビューに同じ幅の画面が表示される動的な水平レイアウトを作成したいと思います。 1つのビューが隠れている場合、残りのビューはそのスペースを埋めるべきです。Android:ダイナミックレイアウトの作成

私はxmlを使用してこれを達成しようとしており、いくつかのコードを書くことは考慮していません。

LinearLayoutと体重を使って画面を埋めるために3つのビューを取得することはできますが、いくつかのビューが隠れている場合はビューがスペースを埋めるようにできません。

レイアウトは次のようになります。

<LinearLayout 
    android:layout_width="match_parent" 
    android:weightSum="3" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="This is bob" /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="This is bob" /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="This is bob" /> 
</LinearLayout> 

私は既に誰かから尋ねられているはずだと思っていましたが、関連する質問は見つかりませんでした。

+0

いただきましエラー????????? – Vijay

+0

は、最初のTextViewをView.INVISIBLEとして可視化します – Vijay

+0

@Vijay:1 TextViewを非表示にすると、残っている2つのテキストビューは66%と33%のスペースが空です。私は残りの2で100%が満たされるように各テキストビューで50%のスペースを取ることを望みます – SAIR

答えて

2

android:orientation="horizontal" 

を追加し、それが動作するメインレイアウトから

android:weightSum="3" 

を削除します。

あなたのレイアウトは次のようにする必要があります:

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

    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="This is bob" /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="This is bob" /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="This is bob" /> 
</LinearLayout> 
+0

それは私の頭の後ろにあり、私はすでにこれをしていましたが、その瞬間には覚えていませんでした。それは最高です。 – SAIR

関連する問題