[英]ListView inside of LinearLayout won't grow to show all children
我正在建立一个从侧面打开的滑动菜单。 在菜单中,有一个ScrollView
,里面有一个LinearLayout
,用于一般布局和滚动。 另外,为了显示菜单项,我在LinearLayout
有一个列表视图,这意味着视图层次结构如下:
ScrollView
LinearLayout
ListView
我正在尝试使用自定义适配器填充ListView
,该适配器从数组中获取其数据。 但令人惊讶的是,当我填充数据时,即使设置为wrap_content
, ListView
也不会增长以显示其所有子wrap_content
。 相反,它只显示一个项目,但我能够滚动该项目以查看其余项目。
我猜它是因为它已经在ScrollView
,但我不想要ListView
的滚动功能。 我只是希望它显示所有菜单项,让ScrollView
负责滚动。 菜单项不会超过10,所以我现在不担心内存使用情况。
有没有办法,可能通过扩展ListView
,禁用滚动并使其真正做wrap_content
?
布局XML:
<ScrollView 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:background="#2e2e2e">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/menu_profile_picture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="10dp"
android:src="@drawable/default_profile_picture"
app:border_color="#FFF"
app:border_width="2dp"/>
<com.devspark.robototextview.widget.RobotoTextView
android:id="@+id/menu_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="John Doe"
android:textColor="#FFF"
android:textSize="22sp"
app:typeface="roboto_thin"/>
<com.devspark.robototextview.widget.RobotoTextView
android:id="@+id/menu_logged_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOGGED IN"
android:textColor="#1e4472"
android:textSize="15sp"
app:typeface="roboto_light"/>
<ListView
android:id="@+id/menu_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="20dp" />
<ImageView
android:id="@+id/menu_logout_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:src="@drawable/ic_logout"/>
<com.devspark.robototextview.widget.RobotoTextView
android:id="@+id/menu_logout_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="LOG OUT"
android:textColor="#e20202"
android:textSize="15sp"
app:typeface="roboto_light"/>
</LinearLayout>
</ScrollView>
一条黄金法则不是将具有滚动功能的视图放入另一个视图。
如果您不想使用ListView
的滚动功能,那么最好使用TableLayout
而不是ListView
并动态添加TableRows
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.