【发布时间】:2015-02-10 00:23:27
【问题描述】:
我想制作一个动态大小的列表视图。当我按下按钮时,它会在列表视图中添加一行。我希望按钮始终与列表视图的底部对齐。
问题是当列表视图有足够的行时,它会将按钮推出屏幕。那么在这种情况下,如何使按钮与屏幕底部对齐或设置列表视图的最大高度?但是当列表视图只有 1 或 2 行时,我希望按钮位于列表视图的正下方。
【问题讨论】:
我想制作一个动态大小的列表视图。当我按下按钮时,它会在列表视图中添加一行。我希望按钮始终与列表视图的底部对齐。
问题是当列表视图有足够的行时,它会将按钮推出屏幕。那么在这种情况下,如何使按钮与屏幕底部对齐或设置列表视图的最大高度?但是当列表视图只有 1 或 2 行时,我希望按钮位于列表视图的正下方。
【问题讨论】:
如果您希望即使用户位于列表视图的顶部也能显示按钮,请参阅其他答案,但如果您希望在用户滚动到列表视图底部时显示按钮,请使用此方法与页脚。
View footer = getLayoutInflater().inflate(R.layout.footer, null);
ListView listView = getListView();
listView.addFooterView(footer);
页脚布局包含按钮。
【讨论】:
你可以在线性布局中加入一个 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>
【讨论】:
根据您的问题,您必须动态更改按钮的位置。从我的评论来看,这就是答案。
Rect videoBounds = new Rect();
yourButton.getGlobalVisibleRect(videoBounds);
getGlobalVisibleRect 将在按钮可见 > 0% 时返回 true,或者返回 false。
alignParentButtom 设置为 true。否则将 alignParentButtom 设置为 false。 之二: 根据您的布局,您必须使用 removeView/addView 在视图树中“移动”您的按钮。不要忘记删除视图,以便在将视图添加到另一个视图之前,该视图将不再有父对象。
附加说明:要在按钮上显示 % 可见性,您可以使用 videoBounds.height() 和三个规则:
100 * videoBounds.height() / yourButton.getHeight
编辑:更改答案以匹配接受的答案。
【讨论】:
Rect videoBounds = new Rect(); yourButton.getGlobalVisibleRect(videoBounds); getGlobalVisibleRect 如果可见 > 0% 将返回 true,否则返回 false。另外,您可以检查videoBounds.height() 的高度并将其与您的按钮高度进行比较,以准确了解按钮可见的程度。让我知道它对你有用,我会在之后立即编辑答案
您可以使用规则 alignParentBottom=true 和 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"/>
【讨论】:
这样的事情应该会有所帮助:
<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>
【讨论】:
抱歉,发表我自己的个人意见,但我认为“添加更多”或:加载更多按钮”始终是 ListView 中的最后一个视图。
要回答您的问题,实现您的需要,Listview 和“添加行”按钮应该是 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 占据了其余的空间。该按钮还与父视图的底部对齐。
【讨论】:
使用相对布局。 将按钮放在屏幕底部,并将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>
【讨论】: