Android LinearLayout ListView的权重和包装内容
我想创建一个两列布局。第一列应该是一个Android LinearLayout ListView的权重和包装内容,android,android-layout,listview,android-linearlayout,Android,Android Layout,Listview,Android Linearlayout,我想创建一个两列布局。第一列应该是一个列表,并且它应该具有他们想要的宽度。第二列应为FrameLayout,并应占据所有剩余空间。 如果我使用以下布局,我可以达到预期效果: <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xml
列表
,并且它应该具有他们想要的宽度。第二列应为FrameLayout
,并应占据所有剩余空间。如果我使用以下布局,我可以达到预期效果:
<?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>
FrameLayout
不见了,ListView
占据所有空间。为什么?
如何使用ListView
而不是RecycleView
从第一个屏幕截图进行布局
Edit1:
这个方案在简单的情况下非常有效。例如,有三个文本视图
,两个包含wrap\u内容
,一个包含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>
Edit3:我可以说得更多:如果您删除第二个示例中的所有内容,只在
ListView
中保留CoordinatorLayout
,则无论内容宽度如何,ListView
仍将全屏显示。为什么?尝试将权重应用到您的列表视图和框架布局中:
<?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>
尝试将权重应用到您的列表视图和框架布局上:
<?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>
在xml中更改listview的代码,如下所示
<ListView
android:id="@+id/list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3" />
当您在线性布局中为第一个子项使用换行内容,在线性布局中为第二个子项使用换行权重时,可能会导致奇怪的行为。换行内容会将空间精确地占用到项目大小,因此在您的情况下,列表可能需要屏幕的所有空间更改listview的代码在xml中,如下所示
<ListView
android:id="@+id/list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3" />
当您在线性布局中为第一个孩子使用换行符内容,在线性布局中为第二个孩子使用换行符权重时,可能会导致奇怪的行为。换行符内容会根据项目大小精确占用空间,因此在您的情况下,列表可能需要屏幕上的所有空间,standart android ListView不能将wrap_content
作为宽度,因为子元素的宽度可能不同。作为一种解决方法,应该使用而不是ListView。正如所说,standart android ListView不能将
wrap_内容作为宽度,因为子元素的宽度可能不同。
作为一种解决方法,您应该使用而不是ListView。请查看我的答案,并让我知道result@Dhiraj评论。不幸的是,这不是我需要的。您可以发布列表项xml吗?可能会有一些宽度=匹配的视图_parent@Exerion我可以看一下你的列表项xml吗?@SAIR编辑的问题,添加的列表项xml请看我的答案,并告诉我关于result@Dhiraj评论。不幸的是,这不是我需要的。您可以发布列表项xml吗?可能会有一些宽度=匹配的视图_parent@Exerion我可以看看你的列表项xml吗?@SAIR编辑的问题,添加了列表项xmlNo,列表内容非常狭窄,但仍然占用了所有空间。您的解决方案只要@Mehta的解决方案使列表始终保持相同的宽度=屏幕宽度的0.3,无论发生什么情况,这不是我需要的。不,列表内容非常窄,但它仍然占用所有空间。你的解决方案只要一个@Mehta的解决方案做一个列表,始终保持相同的宽度=屏幕宽度的0.3,不管怎样,这不是我需要的。