2017-02-22 61 views
0

我現在有如下的佈局:如何在垂直方向的LinearLayout中垂直排列垂直LinearLayout,以便填充屏幕?

enter image description here

突出顯示的佈局是在它三個附加水平linearlayouts(每個文本+搜索條組合)一個LinearLayout中。

我想突出顯示的LinearLayout展開並填充屏幕空間,使「SAVE」按鈕位於屏幕的底部。

我曾嘗試使用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

嘗試將最上面的LinearLayoutandroid:layout_heightmatch_parent - 這是當前設置爲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

您可以使用輔助SPACE視圖解決此問題。

您添加一個帶有權重的空視圖 - 以便強制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>