2016-05-22 14 views
1

Ich versuche ein Layout zu implementieren, das in 2 Unterlayouts aufgeteilt wird. Ich möchte, dass das linke Layout 1/4 des Bildschirms ist, während das rechte die restlichen 3/4 des Bildschirms sein wird. Mein Ziel ist folgendes: Wenn der Benutzer einen Knopf drückt, wird das linke Layout ausgeblendet und das richtige Layout nimmt den gesamten Bildschirm ein (nicht sicher, ob das funktioniert).Android layout_weight funktioniert nicht wie vorgesehen

Um dies zu erreichen, habe ich versucht, das folgende Layout zu verwenden, das nicht wirklich so zu sein scheint.

<?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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="xeniasis.mymarket.MainActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:elevation="4dp" 
     android:padding="5dp" 
     android:theme="@style/ToolbarTheme" /> 

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

     <LinearLayout 
      android:id="@+id/categoriesLayout" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="#000000" 
      android:orientation="vertical"> 

      <!-- added a dummy button to see something --> 
      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 

     <View 
      android:layout_width="2dp" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:background="@android:color/holo_blue_light" /> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:background="#FFFFFF"> 

      <!-- added a dummy button to see something --> 
      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

Wie in der folgenden Abbildung zu sehen ist, ist das Problem, dass die linke LinearLayout nur so breit ist, wie es Inhalt ist und eigentlich nicht 1/4 des Bildschirms einnimmt.

enter image description here

Antwort

2

Ihr Problem ist, dass Sie die Breite des übergeordneten Layout wrap_content gesetzt, und wenn Sie eine volle Bildschirmbreite haben möchten setzen Sie ihn einfach zu match_parent, so wird es so sein:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="xeniasis.mymarket.MainActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:elevation="4dp" 
     android:padding="5dp" 
     android:theme="@style/ToolbarTheme" /> 

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

    . 
    . 
    . 
+0

großartig! das ist es, danke !!! – XeniaSis

+0

Sie sind so willkommen :) –

+1

toll, ich habe dort nicht gesehen. +1 Punkt. –

1

Verwenden android: weightSum = "4" in Ihren Eltern Layout!

können Sie Ihre Layout1 verstecken programmatisch

layout1.setVisibility(View.INVISIBLE); 

Hoffnung nennen, das hilft.

+0

macht keinen Unterschied – XeniaSis

1

Sie benötigen android:weightSum Attribut.

<?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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="xeniasis.mymarket.MainActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:elevation="4dp" 
     android:padding="5dp" 
     android:theme="@style/ToolbarTheme" /> 

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

     <LinearLayout 
      android:id="@+id/categoriesLayout" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".75" 
      android:background="#000000" 
      android:orientation="vertical"> 

      <!-- added a dummy button to see something --> 
      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 

     <View 
      android:layout_width="2dp" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:background="@android:color/holo_blue_light" /> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".25" 
      android:background="#FFFFFF"> 

      <!-- added a dummy button to see something --> 
      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 
+1

Ich bekomme einen Fehler, dass 'android: layout_weightSum' nicht existiert, also ändere es zurück zu' android: weightSum', wie ich in der anderen Antwort kommentierte – XeniaSis

Verwandte Themen