使用Android Studio,我的布局如下:
底部的三个列表项是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
之类的东西。
答案 0 :(得分:0)
解决方案:
ReferenceList
我希望对您有用
答案 1 :(得分:0)
您的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>
要在滚动视图中Scroll
的多个列表视图中,在Java文件中添加以下代码:
public void setListViewHeight(ListView listView) {
ArrayAdapter listAdapter = (ArrayAdapter) listView.getAdapter();
if (listAdapter != null) {
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
如何在Java文件中调用上述方法:
在将adapter
设置为listview
的调用方法后,如下所示:
final ListArrayAdapter adapter3 = new ListArrayAdapter(this,
android.R.layout.simple_list_item_1, arrayList3);
listAssessments.setAdapter(adapter3);
setListViewHeight(listAssessments);
我希望它对您有用。