2017-02-22 1 views
0

私は現在、以下のレイアウトを持っている:親LinearLayout内で垂直LinearLayoutを配置して、画面を塗りつぶすにはどうすればよいですか?

enter image description here

強調表示されたレイアウトは、その中に三つの追加の水平linearlayouts(各テキスト+シークバー組み合わせに対して1つ)とのLinearLayoutです。

強調表示されたLinearLayoutを展開し、画面の下に「保存」ボタンがあるように画面のスペースを埋めることを希望します。

私はandroid:layout_weightを使用しようとしましたが、強調表示されたレイアウトの各子に同じ重みを割り当てましたが、違いはありません。

次は私のレイアウトxmlです:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/add_mood_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="@dimen/activity_horizontal_margin" 
     android:layout_marginStart="@dimen/activity_horizontal_margin" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/because_text" 
      style="@style/TextAppearance.AppCompat.Subhead" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:maxLines="1" 
      android:text="@string/because" /> 

     <EditText 
      android:id="@+id/moodCommentEditText" 
      style="@style/Base.TextAppearance.AppCompat.Body1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginStart="8dp" 
      android:hint="@string/add_a_comment_here" /> 

    </LinearLayout> 

<!-- HIGHLIGHTED LAYOUT --> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:weightSum="3" 
     android:orientation="vertical" 
     android:paddingEnd="@dimen/activity_vertical_margin" 
     android:paddingStart="@dimen/activity_vertical_margin"> 

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

      <TextView 
       android:id="@+id/happy_text" 
       style="@style/TextAppearance.AppCompat.Subhead" 
       tools:text="Unhappy" 
       android:layout_width="65sp" 
       android:layout_height="wrap_content" 
       android:maxLines="1" /> 

      <SeekBar 
       android:id="@+id/happiness_seekbar" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:max="100" 
       android:theme="@style/happinessSeekbarTheme" /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"> 

      <TextView 
       android:id="@+id/stress_text" 
       style="@style/TextAppearance.AppCompat.Subhead" 
       android:layout_width="65sp" 
       android:layout_height="wrap_content" 

       android:maxLines="1" 
       android:text="@string/stress" /> 

      <SeekBar 
       android:id="@+id/stress_seekbar" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:max="100" 
       android:theme="@style/stressSeekbarTheme" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"> 

      <TextView 
       android:id="@+id/pain_text" 
       style="@style/TextAppearance.AppCompat.Subhead" 
       android:layout_width="65sp" 
       android:layout_height="wrap_content" 
       android:maxLines="1" 
       android:text="@string/pain" /> 

      <SeekBar 
       android:id="@+id/pain_seekbar" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:max="100" 
       android:maxHeight="3dp" 
       android:theme="@style/painSeekbarTheme" /> 

     </LinearLayout> 

    </LinearLayout> 

    <Button 
     android:id="@+id/saveButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:background="@android:color/transparent" 
     android:text="@string/save" 
     android:textColor="@color/colorPrimary" 
     android:textSize="15dp" /> 
</LinearLayout> 
+0

あなたはRelativeLayoutを使用してみましたか? – Goran

+0

@damememanの出力がより良い答えになるのに役立ちます –

+0

@Goran私は、マージンとパディングを使用してスペースを詰める唯一の方法を知っている限り、さまざまな画面サイズにわたって予期しない動作を引き起こす可能性があるため、RelativeLayoutを使用しないでください。 – damememan

答えて

4

match_parentに最上位のLinearLayout年代android:layout_heightを設定してみてください - 現在、それはそれを可能に可能な限り最小の高さに縮小することを意味し、wrap_contentに設定されていますその子を適切にレンダリングします。

0
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/add_mood_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="@dimen/activity_horizontal_margin" 
    android:layout_marginStart="@dimen/activity_horizontal_margin" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/because_text" 
     style="@style/TextAppearance.AppCompat.Subhead" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:maxLines="1" 
     android:text="@string/because" /> 

    <EditText 
     android:id="@+id/moodCommentEditText" 
     style="@style/Base.TextAppearance.AppCompat.Body1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginStart="8dp" 
     android:layout_marginLeft="8dp" 
     android:hint="@string/add_a_comment_here" /> 

</LinearLayout> 

<!-- HIGHLIGHTED LAYOUT --> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    android:paddingEnd="@dimen/activity_vertical_margin" 
    android:paddingStart="@dimen/activity_vertical_margin"> 

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

     <TextView 
      android:id="@+id/happy_text" 
      style="@style/TextAppearance.AppCompat.Subhead" 
      tools:text="Unhappy" 
      android:layout_width="65sp" 
      android:layout_height="wrap_content" 
      android:maxLines="1" /> 

     <SeekBar 
      android:id="@+id/happiness_seekbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:max="100" 
      android:theme="@style/happinessSeekbarTheme" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

     <TextView 
      android:id="@+id/stress_text" 
      style="@style/TextAppearance.AppCompat.Subhead" 
      android:layout_width="65sp" 
      android:layout_height="wrap_content" 

      android:maxLines="1" 
      android:text="@string/stress" /> 

     <SeekBar 
      android:id="@+id/stress_seekbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:max="100" 
      android:theme="@style/stressSeekbarTheme" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

     <TextView 
      android:id="@+id/pain_text" 
      style="@style/TextAppearance.AppCompat.Subhead" 
      android:layout_width="65sp" 
      android:layout_height="wrap_content" 
      android:maxLines="1" 
      android:text="@string/pain" /> 

     <SeekBar 
      android:id="@+id/pain_seekbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:max="100" 
      android:maxHeight="3dp" 
      android:theme="@style/painSeekbarTheme" /> 

    </LinearLayout> 

</LinearLayout> 

<Button 
    android:id="@+id/saveButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:background="@android:color/transparent" 
    android:text="@string/save" 
    android:textColor="@color/colorPrimary" 
    android:textSize="15sp" /> 
</LinearLayout> 
0

この問題は、ヘルパースペースビューで解決できます。

重さを持つ空のビューを追加すると、LinearLayoutの次のビューが強制的に最後に配置されます。

注: Androidはその目的Spaceのための特別なビューがありますが、そのようにそれが唯一のAPIレベルで14

の中に得た:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/add_mood_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" <!-- Also changed this --> 
    android:orientation="vertical"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="@dimen/activity_horizontal_margin" 
    android:layout_marginStart="@dimen/activity_horizontal_margin" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/because_text" 
     style="@style/TextAppearance.AppCompat.Subhead" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:maxLines="1" 
     android:text="@string/because" /> 

    <EditText 
     android:id="@+id/moodCommentEditText" 
     style="@style/Base.TextAppearance.AppCompat.Body1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginStart="8dp" 
     android:hint="@string/add_a_comment_here" /> 

</LinearLayout> 

<!-- HIGHLIGHTED LAYOUT --> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="3" 
    android:orientation="vertical" 
    android:paddingEnd="@dimen/activity_vertical_margin" 
    android:paddingStart="@dimen/activity_vertical_margin"> 

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

     <TextView 
      android:id="@+id/happy_text" 
      style="@style/TextAppearance.AppCompat.Subhead" 
      tools:text="Unhappy" 
      android:layout_width="65sp" 
      android:layout_height="wrap_content" 
      android:maxLines="1" /> 

     <SeekBar 
      android:id="@+id/happiness_seekbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:max="100" 
      android:theme="@style/happinessSeekbarTheme" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

     <TextView 
      android:id="@+id/stress_text" 
      style="@style/TextAppearance.AppCompat.Subhead" 
      android:layout_width="65sp" 
      android:layout_height="wrap_content" 

      android:maxLines="1" 
      android:text="@string/stress" /> 

     <SeekBar 
      android:id="@+id/stress_seekbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:max="100" 
      android:theme="@style/stressSeekbarTheme" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

     <TextView 
      android:id="@+id/pain_text" 
      style="@style/TextAppearance.AppCompat.Subhead" 
      android:layout_width="65sp" 
      android:layout_height="wrap_content" 
      android:maxLines="1" 
      android:text="@string/pain" /> 

     <SeekBar 
      android:id="@+id/pain_seekbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:max="100" 
      android:maxHeight="3dp" 
      android:theme="@style/painSeekbarTheme" /> 

    </LinearLayout> 

</LinearLayout> 

<!-- Space View: Add this view here for white space --> 
<View 
    android:layout_width="match_parent" 
    android:layout_height="0px" 
    android:layout_weight="1"/> 

<Button 
    android:id="@+id/saveButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:background="@android:color/transparent" 
    android:text="@string/save" 
    android:textColor="@color/colorPrimary" 
    android:textSize="15dp" /> 
</LinearLayout> 
関連する問題