Android - 如何设置列表视图的最大大小
Android - How to set maximum size for listview
我想制作一个动态大小的列表视图。当我按下按钮时,它会在列表视图中添加一行。我希望按钮始终与列表视图的底部对齐。
问题是列表视图在有足够的行时会将按钮推出屏幕。那么在这种情况下,如何使按钮与屏幕底部对齐或设置列表视图的最大高度?但是当列表视图只有 1 或 2 行时,我希望按钮位于列表视图的正下方。
您可以使用规则 alignParentBottom=true 为 Button 创建一个 RelativeLayout,使用上述规则为 ListView 创建一个 RelativeLayout。像这样:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/button" >
</ListView>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"/>
根据您的问题,您必须动态更改按钮的位置。根据我的评论,这就是答案。
- 将侦听器附加到您的 ListView :请参阅
- 在侦听器回调中,检查按钮的可见性:
Rect videoBounds = new Rect();
yourButton.getGlobalVisibleRect(videoBounds);
如果按钮可见度 > 0%,getGlobalVisibleRect
将 return 为真,否则为假。
- 要将按钮固定在底部:更改按钮的 LayoutParams 并将
alignParentButtom
设置为 true。否则将 alignParentButtom
设置为 false。
bis : 根据您的布局,您必须 "move" 在视图树中使用 removeView/addView。不要忘记删除视图,这样在将视图添加到另一个视图之前,视图将不再有父视图。
补充说明: 要在您的按钮上显示 %,您可以使用 videoBounds.height()
和三个规则:
100 * videoBounds.height() / yourButton.getHeight
编辑:更改答案以匹配接受的答案。
这样的事情应该有所帮助:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Add"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/add"
/>
</RelativeLayout>
如果您希望即使用户位于列表视图的顶部也显示按钮请参阅其他答案,但如果您希望在用户滚动到列表视图底部时显示按钮,请使用此方法带页脚。
View footer = getLayoutInflater().inflate(R.layout.footer, null);
ListView listView = getListView();
listView.addFooterView(footer);
页脚布局包含按钮。
抱歉,要表达我自己的个人观点,但我认为 "add more" 或 :load more button" 始终是 ListView 中的最后一个视图。
为了回答您的问题,实现您所需要的,Listview 和 "add row" 按钮应该是 ViewGroup 中的兄弟姐妹。
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="48dp"
android:minHeight="48dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
这里的关键点是Button有一个固定的高度,ListView占据了剩下的space。该按钮还与父视图的底部对齐。
使用相对布局。
将按钮放在屏幕底部,并将listView位置设置在他上面:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:layout_width="match_parent"
android:id="@+id/list"
android:layout_above="@+id/button1"
android:layout_height="wrap_content">
</ListView>
<Button
android:layout_width="match_parent"
android:id="@+id/button1"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content" />
</RelativeLayout>
您可以在线性布局中加入一个 ListView 和一个具有不同权重的按钮。像这样
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/reportCommentsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/reportCommentsListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:paddingTop="5dp" >
</ListView>
<Button
android:id="@+id/reportCommnets_Addbutton"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:text="Button" />
</LinearLayout>
我想制作一个动态大小的列表视图。当我按下按钮时,它会在列表视图中添加一行。我希望按钮始终与列表视图的底部对齐。
问题是列表视图在有足够的行时会将按钮推出屏幕。那么在这种情况下,如何使按钮与屏幕底部对齐或设置列表视图的最大高度?但是当列表视图只有 1 或 2 行时,我希望按钮位于列表视图的正下方。
您可以使用规则 alignParentBottom=true 为 Button 创建一个 RelativeLayout,使用上述规则为 ListView 创建一个 RelativeLayout。像这样:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/button" >
</ListView>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"/>
根据您的问题,您必须动态更改按钮的位置。根据我的评论,这就是答案。
- 将侦听器附加到您的 ListView :请参阅
- 在侦听器回调中,检查按钮的可见性:
Rect videoBounds = new Rect();
yourButton.getGlobalVisibleRect(videoBounds);
getGlobalVisibleRect
将 return 为真,否则为假。
- 要将按钮固定在底部:更改按钮的 LayoutParams 并将
alignParentButtom
设置为 true。否则将alignParentButtom
设置为 false。
bis : 根据您的布局,您必须 "move" 在视图树中使用 removeView/addView。不要忘记删除视图,这样在将视图添加到另一个视图之前,视图将不再有父视图。
补充说明: 要在您的按钮上显示 %,您可以使用 videoBounds.height()
和三个规则:
100 * videoBounds.height() / yourButton.getHeight
编辑:更改答案以匹配接受的答案。
这样的事情应该有所帮助:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Add"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/add"
/>
</RelativeLayout>
如果您希望即使用户位于列表视图的顶部也显示按钮请参阅其他答案,但如果您希望在用户滚动到列表视图底部时显示按钮,请使用此方法带页脚。
View footer = getLayoutInflater().inflate(R.layout.footer, null);
ListView listView = getListView();
listView.addFooterView(footer);
页脚布局包含按钮。
抱歉,要表达我自己的个人观点,但我认为 "add more" 或 :load more button" 始终是 ListView 中的最后一个视图。
为了回答您的问题,实现您所需要的,Listview 和 "add row" 按钮应该是 ViewGroup 中的兄弟姐妹。
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="48dp"
android:minHeight="48dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
这里的关键点是Button有一个固定的高度,ListView占据了剩下的space。该按钮还与父视图的底部对齐。
使用相对布局。 将按钮放在屏幕底部,并将listView位置设置在他上面:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:layout_width="match_parent"
android:id="@+id/list"
android:layout_above="@+id/button1"
android:layout_height="wrap_content">
</ListView>
<Button
android:layout_width="match_parent"
android:id="@+id/button1"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content" />
</RelativeLayout>
您可以在线性布局中加入一个 ListView 和一个具有不同权重的按钮。像这样
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/reportCommentsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/reportCommentsListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:paddingTop="5dp" >
</ListView>
<Button
android:id="@+id/reportCommnets_Addbutton"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:text="Button" />
</LinearLayout>