2017-02-22 1 views
0

Ich habe derzeit folgende Layout:Wie platziere ich ein vertikales LinearLayout innerhalb eines übergeordneten vertikalen LinearLayouts, damit es den Bildschirm ausfüllt?

enter image description here

Der markierte Layout ist ein Linearlayout mit drei zusätzlichen horizontalen linearlayouts darin (eine für jeden Text + SeekBar Kombination).

Ich möchte das hervorgehobene LinearLayout erweitern und füllen Sie den Raum des Bildschirms, so dass die Schaltfläche "Speichern" ist am unteren Rand des Bildschirms.

Ich habe versucht, android:layout_weight zu verwenden und jedem der Kinder des hervorgehobenen Layouts das gleiche Gewicht zuzuordnen, aber es scheint keinen Unterschied zu machen.

Das folgende ist mein Layout 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

Versuchen Sie RelativeLayout mit? – Goran

+0

@damememan erwartete Ausgabe wird helfen, eine bessere Antwort zu geben –

+0

@Goran Ich möchte vermeiden, RelativeLayout zu verwenden, denn soweit ich weiß, die einzige Möglichkeit, Dinge zu spreizen ist mit Margen und Padding, die zu unvorhersehbarem Verhalten über verschiedene Bildschirmgrößen führen kann. – damememan

Antwort

4

Versuchen die oberste LinearLayout Einstellung ‚s android:layout_height zu match_parent - es derzeit wrap_content gesetzt ist, was bedeutet, dass es auf die kleinstmögliche Höhe schrumpfen wird, die es ermöglicht, richtig seine Kinder rendern.

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

Sie können dieses Problem mit einer Hilfsansicht SPACE lösen.

Sie fügen eine leere Ansicht mit einem Gewicht hinzu - damit die folgende Ansicht im LinearLayout am Ende positioniert wird.

Hinweis: Android hat eine spezielle Ansicht zu diesem Zweck Space, aber es wurde nur in API-Ebene in 14

Wie so:

<?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> 
Verwandte Themen