Android 可滚动父布局内部的不可滚动ListView
使用Android Studio,我有以下布局:Android 可滚动父布局内部的不可滚动ListView,android,android-layout,scrollview,Android,Android Layout,Scrollview,使用Android Studio,我有以下布局: 底部的三个列表项是ListView对象,它们都位于RelativeLayout对象的内部,该对象位于ScrollView对象的内部。我希望这三个列表视图对象的布局高度能够包装内容,并且不可滚动。如果三个列表对象的高度超出屏幕底部,我希望RelativeLayout可滚动,但不希望ListView对象滚动 如果您需要,以下是此活动的代码: <?xml version="1.0" encoding="utf-8"?> <Rela
底部的三个列表项是
ListView
对象,它们都位于RelativeLayout
对象的内部,该对象位于ScrollView
对象的内部。我希望这三个列表视图对象的布局高度能够包装内容,并且不可滚动。如果三个列表对象的高度超出屏幕底部,我希望RelativeLayout
可滚动,但不希望ListView
对象滚动
如果您需要,以下是此活动的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:padding="@dimen/root_padding"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CourseViewActivity">
<TextView
android:id="@+id/title_display"
android:text="@string/course_view"
android:textSize="@dimen/header_text_size"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/date_display"
android:text="@string/start_and_end_dates"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_below="@id/title_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/mentor_display"
android:text="@string/course_mentor_display_default"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_below="@id/date_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/credits_display"
android:text="@string/course_credits_display_default"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_below="@id/mentor_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/passed_display"
android:text="@string/passed"
android:textColor="#128039"
android:textStyle="bold"
android:textSize="@dimen/sub_header_text_size"
android:visibility="gone"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_above="@id/separator"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/edit_course_button"
android:text="@string/edit_course"
android:onClick="editCourse"
android:background="@color/blue_button_bg"
android:textColor="@color/blue_button_fg"
android:padding="@dimen/button_padding"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/separator"
android:layout_below="@id/credits_display"
android:background="#333"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="2dp" />
<ScrollView
android:layout_below="@id/separator"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/objective_header"
android:text="@string/objectives"
android:textSize="@dimen/sub_header_text_size"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/create_objective_button"
android:onClick="createNewObjective"
android:src="@drawable/ic_add"
android:layout_alignTop="@+id/objective_header"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/objective_list"
android:layout_below="@id/objective_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/assessment_header"
android:text="@string/assessments"
android:textSize="@dimen/sub_header_text_size"
android:layout_below="@id/objective_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/create_assessment_button"
android:onClick="createNewAssessments"
android:src="@drawable/ic_add"
android:layout_alignTop="@+id/assessment_header"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/assessment_list"
android:layout_below="@id/assessment_header"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/note_header"
android:text="@string/notes"
android:textSize="@dimen/sub_header_text_size"
android:layout_below="@id/assessment_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/create_note_button"
android:onClick="createNewNote"
android:src="@drawable/ic_add"
android:layout_alignTop="@+id/note_header"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/note_list"
android:layout_below="@id/note_header"
android:layout_marginBottom="10dp"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
编辑:
该项目需要API 15,这意味着我不能使用类似于
RecyclerView
解决方案:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:padding="@dimen/_5sdp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativeLayout">
<TextView
android:id="@+id/title_display"
android:text="@string/course_view"
android:textSize="@dimen/header_text_size"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/date_display"
android:text="@string/start_and_end_dates"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_below="@id/title_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/mentor_display"
android:text="@string/course_mentor_display_default"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_below="@id/date_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/credits_display"
android:text="@string/course_credits_display_default"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_below="@id/mentor_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/passed_display"
android:text="@string/passed"
android:textColor="#128039"
android:textStyle="bold"
android:textSize="@dimen/sub_header_text_size"
android:visibility="gone"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_above="@id/separator"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/edit_course_button"
android:text="@string/edit_course"
android:onClick="editCourse"
android:background="@color/blue_button_bg"
android:textColor="@color/blue_button_fg"
android:padding="@dimen/button_padding"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/separator"
android:layout_below="@id/credits_display"
android:background="#333"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="2dp" />
</RelativeLayout>
<ScrollView
android:layout_below="@id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/objective_header"
android:text="@string/objectives"
android:textSize="@dimen/sub_header_text_size"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/create_objective_button"
android:onClick="createNewObjective"
android:src="@drawable/ic_add"
android:layout_alignTop="@+id/objective_header"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/objective_list"
android:layout_below="@id/objective_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/assessment_header"
android:text="@string/assessments"
android:textSize="@dimen/sub_header_text_size"
android:layout_below="@id/objective_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/create_assessment_button"
android:onClick="createNewAssessments"
android:src="@drawable/ic_add"
android:layout_alignTop="@+id/assessment_header"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/assessment_list"
android:layout_below="@id/assessment_header"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/note_header"
android:text="@string/notes"
android:textSize="@dimen/sub_header_text_size"
android:layout_below="@id/assessment_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/create_note_button"
android:onClick="createNewNote"
android:src="@drawable/ic_add"
android:layout_alignTop="@+id/note_header"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/note_list"
android:layout_below="@id/note_header"
android:layout_marginBottom="10dp"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
</RelativeLayout>
我希望这适用于您您的xml代码是正确的,但需要一些更改检查以下代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".SwipeRefreshLayoutActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title_display"
android:text="@string/course_view"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/date_display"
android:text="@string/start_and_end_dates"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_below="@id/title_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/mentor_display"
android:text="@string/course_mentor_display_default"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_below="@id/date_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/credits_display"
android:text="@string/course_credits_display_default"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_below="@id/mentor_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/passed_display"
android:text="@string/passed"
android:textColor="#128039"
android:textStyle="bold"
android:visibility="gone"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_above="@id/separator"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/edit_course_button"
android:text="@string/edit_course"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/separator"
android:layout_below="@id/credits_display"
android:background="#333"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="2dp" />
</RelativeLayout>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Objectives"
android:layout_centerVertical="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_arrow_left"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
<ListView
android:id="@+id/list_objectives"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Assessments"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_arrow_left"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
<ListView
android:id="@+id/list_assessments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Notes"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_arrow_left"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
<ListView
android:id="@+id/list_notes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
如何在java文件中调用上述方法:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:padding="@dimen/_5sdp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativeLayout">
<TextView
android:id="@+id/title_display"
android:text="@string/course_view"
android:textSize="@dimen/header_text_size"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/date_display"
android:text="@string/start_and_end_dates"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_below="@id/title_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/mentor_display"
android:text="@string/course_mentor_display_default"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_below="@id/date_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/credits_display"
android:text="@string/course_credits_display_default"
android:layout_toLeftOf="@id/edit_course_button"
android:layout_toStartOf="@id/edit_course_button"
android:layout_below="@id/mentor_display"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/passed_display"
android:text="@string/passed"
android:textColor="#128039"
android:textStyle="bold"
android:textSize="@dimen/sub_header_text_size"
android:visibility="gone"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_above="@id/separator"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/edit_course_button"
android:text="@string/edit_course"
android:onClick="editCourse"
android:background="@color/blue_button_bg"
android:textColor="@color/blue_button_fg"
android:padding="@dimen/button_padding"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/separator"
android:layout_below="@id/credits_display"
android:background="#333"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="2dp" />
</RelativeLayout>
<ScrollView
android:layout_below="@id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/objective_header"
android:text="@string/objectives"
android:textSize="@dimen/sub_header_text_size"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/create_objective_button"
android:onClick="createNewObjective"
android:src="@drawable/ic_add"
android:layout_alignTop="@+id/objective_header"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/objective_list"
android:layout_below="@id/objective_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/assessment_header"
android:text="@string/assessments"
android:textSize="@dimen/sub_header_text_size"
android:layout_below="@id/objective_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/create_assessment_button"
android:onClick="createNewAssessments"
android:src="@drawable/ic_add"
android:layout_alignTop="@+id/assessment_header"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/assessment_list"
android:layout_below="@id/assessment_header"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/note_header"
android:text="@string/notes"
android:textSize="@dimen/sub_header_text_size"
android:layout_below="@id/assessment_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/create_note_button"
android:onClick="createNewNote"
android:src="@drawable/ic_add"
android:layout_alignTop="@+id/note_header"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/note_list"
android:layout_below="@id/note_header"
android:layout_marginBottom="10dp"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
</RelativeLayout>
将适配器
设置为列表视图
调用方法后,如下所示:
final ListArrayAdapter adapter3 = new ListArrayAdapter(this,
android.R.layout.simple_list_item_1, arrayList3);
listAssessments.setAdapter(adapter3);
setListViewHeight(listAssessments);
我希望它对您有用。“我希望这三个列表视图对象的布局高度能够包装内容,并且不可滚动”——然后不要使用
列表视图。使用ConstraintLayout
或LinearLayout
包装各个“列表”行。或者,摆脱所有这些,使用一个包含所有可滚动内容的RecyclerView
。我忘了提到项目需要API 15,这意味着我不能使用类似RecyclerView
的东西。至于使用线性布局
而不是列表视图
,我认为这可能是我需要采取的方式,但我不确定如何做到这一点,但我能找到答案。我只是希望有一个简单的方法可以像使用ListView
“我忘了提到这个项目需要API 15,这意味着我不能使用像RecyclerView这样的东西。”---.ScrollView应该有固定的布局,因此不建议在高度上使用wrap_content
尝试将其更改为match_parent
并且ListView在ScrollView内部(这是常见的情况)时失去了滚动的能力。您更改了什么?我正在查看,但没有看到任何更改。