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:
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>
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:
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 ListView
immer nimmt Vollbild unabhängig von der Breite von wrap_content. Warum?
Bitte sehen Sie meine Antwort und lassen Sie mich wissen, Ergebnis – Dhiraj
@Dhiraj kommentierte. Leider brauche ich das nicht. – Exerion
können Sie den Listeneintrag xml posten? könnte sein dort würde eine Ansicht sein, die width hat = match_parent – SAIR