[英]How can I include listView in a layout with other views in the layout already
I have an activity with ImageView
on top and TextView
in bottom.我有一个活动,顶部有ImageView
,底部有TextView
。 I want to add a ListView
in between, How can i do that?我想在两者之间添加一个ListView
,我该怎么做?
Take linear layout as parent layout and give its orientation vertical.将线性布局作为父布局并使其方向垂直。 Add imageview, listview, textview sequentially.依次添加 imageview、listview、textview。 Now provide weight to listview as 1 and its height as 0dp.现在将列表视图的权重设为 1,将其高度设为 0dp。 Providing weight 1 to listview will allow it to spread in the whole empty space of the layout.为列表视图提供权重 1 将允许它在布局的整个空白空间中传播。 Hope this helps希望这可以帮助
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp" />
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
You can use ConstraintLayout as parent and a RecyclerView instead of ListView.您可以使用ConstraintLayout作为父级和RecyclerView而不是 ListView。
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image"
app:layout_constraintBottom_toTopOf="@+id/textview"/>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.