2016-05-31 23 views
2

Ich möchte ein zweispaltiges Layout erstellen. Die erste Spalte sollte eine List sein, und es sollte so viel Breite nehmen, wie sie wollen. Die zweite Spalte sollte FrameLayout lauten und sollte den gesamten verbleibenden Speicherplatz belegen.
Ich kann das gewünschte erreichen, wenn ich das folgende Layout verwenden:Android LinearLayout Gewicht und wrap_content von ListView

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="ru.automatikaplus.caliberadmin.TestActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:baselineAligned="false" 
     android:divider="?android:attr/dividerHorizontal" 
     android:orientation="horizontal" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/list" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="16dp" 
      android:layout_marginRight="16dp" /> 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@color/colorGreen"/> 

    </LinearLayout> 


</android.support.design.widget.CoordinatorLayout> 

Und das Ergebnis:

Screenshot_1

FrameLayout für Klarheit grün ist.

ABER! Wenn ich RecyclerView-ListView oder irgendetwas anderes zu ändern - das Layout nicht funktioniert:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="ru.automatikaplus.caliberadmin.TestActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:baselineAligned="false" 
     android:divider="?android:attr/dividerHorizontal" 
     android:orientation="horizontal" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <ListView 
      android:id="@+id/list" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="16dp" 
      android:layout_marginRight="16dp" /> 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@color/colorGreen"/> 

    </LinearLayout> 


</android.support.design.widget.CoordinatorLayout> 

enter image description here

FrameLayout ist weg, ListView den ganzen Raum übernehmen. Warum?

Wie kann ich Layout von ersten Screenshot mit ListView anstelle von RecycleView machen?

Edit1: Dieses Schema funktioniert perfekt in einfachen Fällen. Zum Beispiel gibt es drei TextViews, zwei mit wrap_content und einem mit weight=1:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="20sp" 
     android:text="Test1"/> 

    <TextView 
     android:id="@+id/text2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:textSize="20sp" 
     android:layout_weight="1" 
     android:background="@color/colorGreen" 
     android:text="Test2"/> 

    <TextView 
     android:id="@+id/text3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="20sp" 
     android:text="Test3"/> 

</LinearLayout> 

Es wirkt wie ein Zauber, Mitte TextView all freien Platz in Anspruch nimmt:

enter image description here

Aber das funktioniert nicht mit ListView. Ich brauche ListView, um nur Platz zu nehmen, den er braucht, nicht 0,3 von Bildschirm, nicht fest codierten Wert von dp, nur ein wrap_content.

Edit2: Liste item Layout ist wie folgt vor:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:textSize="18sp"/> 

Edit3: kann ich Ihnen mehr sagen: Wenn Sie alle Sachen aus dem zweiten Beispiel entfernen und nur CoordinatorLayout mit ListView Inneren lassen, die ListViewimmer nimmt Vollbild unabhängig von der Breite von wrap_content. Warum?

+0

Bitte sehen Sie meine Antwort und lassen Sie mich wissen, Ergebnis – Dhiraj

+0

@Dhiraj kommentierte. Leider brauche ich das nicht. – Exerion

+0

können Sie den Listeneintrag xml posten? könnte sein dort würde eine Ansicht sein, die width hat = match_parent – SAIR

Antwort

0

erfordert, wie es gesagt wird in here und in here, standart Android Listview nicht wrap_content als Breite aufweisen kann, da anders die Breite der untergeordneten Elemente sein können.
Als Workaround sollte man anstelle von ListView RecycleView verwenden.

-1

dies versuchen, Gewicht zu Ihrem Listview und Rahmenlayout Anwendung:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout > 

    <android.support.design.widget.AppBarLayout > 
     <android.support.v7.widget.Toolbar/> 
    </android.support.design.widget.AppBarLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:baselineAligned="false" 
     android:divider="?android:attr/dividerHorizontal" 
     android:orientation="horizontal" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <ListView 
      android:id="@+id/list" 
      android:layout_width="0dp" 
      android:layout_weight="0.3" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="16dp" 
      android:layout_marginRight="16dp" /> 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.7" 
      android:background="@color/colorGreen"/> 

    </LinearLayout> 


</android.support.design.widget.CoordinatorLayout> 
0

ändern Sie den Code für Listview in xml als

<ListView 
       android:id="@+id/list" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="0.3" /> 

folgt Wie Sie wrap_content für erste verwenden Kind im linearen Layout und layout_weight für das zweite Kind im linearen Layout, so dass es möglicherweise zu einem seltsamen Verhalten führen kann. WRAP_CONTENT nimmt den Raum genau zum Gegenstand Größe so in Ihrem Fall könnte es möglich sein, dass die Liste den ganzen Raum des Bildschirms

+0

Nein, Liste Inhalt ist sehr eng, aber es braucht immer noch Platz. Deine Lösung, solange eine @ Mehta-Lösung eine Liste immer gleich breit macht = 0.3 der Breite des Bildschirms egal was, das brauche ich nicht. – Exerion

Verwandte Themen