Java 列表视图-自动修改高度
我正在学习Android,现在我有了这个教程: 我修改(我想滚动所有页面)此项,我有:Java 列表视图-自动修改高度,java,android,Java,Android,我正在学习Android,现在我有了这个教程: 我修改(我想滚动所有页面)此项,我有: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ScrollView01" android:layout_width="wrap_content" andr
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/petList"
android:layout_width="wrap_content"
android:layout_height="700dp" //here!!!!
android:divider="#000"
/>
<Button
android:id="@+id/ButtonAddNews"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add new Pet" >
</Button>
</LinearLayout>
</ScrollView>
单项:
<LinearLayout
android:id="@+id/relative"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="5dp"
orientation="horizontal">
<TextView
android:id="@+id/TextView01"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#f00"
android:layout_alignParentLeft="true"
android:text="two" ></TextView>
<TextView
android:id="@+id/TextView02"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#f00"
android:text="three"
android:layout_alignParentRight="true"></TextView>
</LinearLayout>
这工作正常,但我必须修改android:layout\u height
例如,若我在列表中有3项,那个么700dp就太多了。
如果我使用fill\u parent、match\u parent或wrap\u内容,那么这只会显示一个项目(我可以滚动它)
我必须在布局中键入什么内容才能自动展开,以及项目的数量?首先:删除该滚动视图!!切勿将
列表视图
放在滚动视图
内!谷歌搜索“android listview inside scrollview”以获取更多信息
现在,通常您会以如下方式实现该布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/petList"
android:layout_width="wrap_content"
android:layout_height="0dp" // set layout_height=0dp
android:layout_weight="1" // add layout_weight=1
android:divider="#000"
/>
<Button
android:id="@+id/ButtonAddNews"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add new Pet" >
</Button>
</LinearLayout>
这样,列表将使用整个可用高度,按钮使用的高度除外。如果您希望按钮与列表一起滚动,请使用列表页脚。使ScrollView和LinearLayoutPill的android:layout\u height=“fill\u parent”都无法工作。你能举个例子吗?也许我不适合。好吧,高度现在起作用了,但滚动不起作用。例如,如果我有15个项目,那么数字10之后的项目位于屏幕下方。我想用按钮滚动所有页面。
列表视图
是一个滚动视图
,因此它应该自己滚动。如果希望该按钮成为列表的一部分并与其一起滚动,请将其添加为列表页脚。在谷歌的帮助下,你会明白如何去做。