Android-如何设置listview的最大大小

Android-如何设置listview的最大大小,android,android-listview,Android,Android Listview,我想制作一个动态大小的列表视图。当我按下按钮时,它将向listview添加一行。我希望按钮始终与列表视图的底部对齐 问题是当listview有足够的行时,它会将按钮推出屏幕。那么在这种情况下,如何使按钮与屏幕底部对齐或设置listview的最大高度?但是,当listview只有1或2行时,我希望按钮就在listview的正下方。您可以使用规则alignParentBottom=true对按钮进行RelativeLayout,并使用上面的规则对listview进行RelativeLayout。大概

我想制作一个动态大小的列表视图。当我按下按钮时,它将向listview添加一行。我希望按钮始终与列表视图的底部对齐


问题是当listview有足够的行时,它会将按钮推出屏幕。那么在这种情况下,如何使按钮与屏幕底部对齐或设置listview的最大高度?但是,当listview只有1或2行时,我希望按钮就在listview的正下方。

您可以使用规则alignParentBottom=true对按钮进行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
    如果按钮可见>0%,则返回true,否则返回false

  • 要修复底部的按钮:更改按钮的布局参数,并将
    alignParentButtom
    设置为true。否则将
    alignParentButtom
    设置为false bis:根据您的布局,您必须使用removeView/addView在视图树中“移动”按钮。在将视图添加到另一个视图之前,不要忘记删除视图,这样该视图就不会有父视图naymore

    附加说明:要使按钮具有%的可见性,可以使用
    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);
    

    页脚布局包含按钮。

    对不起,我想插入我个人的观点,但我认为“添加更多”或“加载更多”按钮始终是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>
    
    
    
    这里的关键点是按钮具有固定的高度,而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>
    

    您可以将一个列表视图和一个不同权重的按钮放入线性布局中

    <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>
    
    
    
    没问题。但是当listview只有1行或2行时,我希望按钮位于listview的正下方。您的解决方案使按钮始终在底部对齐。我只希望当listview向外推时按钮与底部屏幕对齐。如果listview只有1-2行,则按钮必须位于listview的正下方。您必须附加一个listen返回到Scroll上的listview,检查按钮的可见性是否低于100%,从按钮中获取父项并移除视图(yourButton),将按钮添加到正确的视图中,并更改布局参数以匹配alignParentButtomDear Hugo,谢谢您的帮助。但是如何检查视图的可见性百分比。
    Rect videoBounds=new Rect();yourButton.getGlobalVisibleRect(videoBounds);
    getGlobalVisibleRect将在可见>0%时返回true,也将返回false。此外,您可以检查
    videoBounds.height()的高度
    并将其与您的按钮高度进行比较,以准确了解按钮的可视程度。如果它对您有效,请告诉我,我会立即编辑答案亲爱的雨果,看起来没问题。但我明天会检查它,因为现在是上午12点,我忙了一天。非常感谢。谢谢您的帮助。但我的意思是按钮的优先级:1-Below列表视图,如果它有空间。2-对齐屏幕的底部,如果列表视图将其向外推。