【问题标题】:Android - How to set maximum size for listviewAndroid - 如何设置列表视图的最大尺寸
【发布时间】:2015-02-10 00:23:27
【问题描述】:

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

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

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    如果您希望即使用户位于列表视图的顶部也能显示按钮,请参阅其他答案,但如果您希望在用户滚动到列表视图底部时显示按钮,请使用此方法与页脚。

    View footer = getLayoutInflater().inflate(R.layout.footer, null);
    ListView listView = getListView();
    listView.addFooterView(footer);
    

    页脚布局包含按钮。

    【讨论】:

      【解决方案2】:

      你可以在线性布局中加入一个 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>
      

      【讨论】:

        【解决方案3】:

        根据您的问题,您必须动态更改按钮的位置。从我的评论来看,这就是答案。

        1. 将侦听器附加到您的 ListView :请参阅 https://stackoverflow.com/a/4433294/1377145
        2. 在侦听器回调中,检查按钮的可见性:

        Rect videoBounds = new Rect(); yourButton.getGlobalVisibleRect(videoBounds);

        getGlobalVisibleRect 将在按钮可见 > 0% 时返回 true,或者返回 false。

        1. 要修复底部的按钮:更改按钮的 LayoutParams 并将 alignParentButtom 设置为 true。否则将 alignParentButtom 设置为 false。

        之二: 根据您的布局,您必须使用 removeView/addView 在视图树中“移动”您的按钮。不要忘记删除视图,以便在将视图添加到另一个视图之前,该视图将不再有父对象。

        附加说明:要在按钮上显示 % 可见性,您可以使用 videoBounds.height() 和三个规则:

        100 * videoBounds.height() / yourButton.getHeight
        

        编辑:更改答案以匹配接受的答案。

        【讨论】:

        • 我只希望按钮在列表视图向外推出时与底部屏幕对齐。如果列表视图只有 1-2 行,则按钮必须位于列表视图的正下方
        • 你必须在listview onScroll上附加一个监听器,检查你的按钮的可见性是否小于100%,从你的按钮和removeView(yourButton)中获取父级并将你的按钮添加到正确的视图中并更改 LayoutParams 以匹配 alignParentButtom
        • 亲爱的雨果,感谢您的帮助。但是如何检查视图的可见性百分比。
        • Rect videoBounds = new Rect(); yourButton.getGlobalVisibleRect(videoBounds); getGlobalVisibleRect 如果可见 > 0% 将返回 true,否则返回 false。另外,您可以检查videoBounds.height() 的高度并将其与您的按钮高度进行比较,以准确了解按钮可见的程度。让我知道它对你有用,我会在之后立即编辑答案
        • 亲爱的雨果,看起来没问题。但我明天会检查它,因为现在是上午 12 点,我度过了忙碌的一天。非常感谢。
        【解决方案4】:

        您可以使用规则 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"/>
        

        【讨论】:

          【解决方案5】:

          这样的事情应该会有所帮助:

              <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>
          

          【讨论】:

            【解决方案6】:

            抱歉,发表我自己的个人意见,但我认为“添加更多”或:加载更多按钮”始终是 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 占据了其余的空间。该按钮还与父视图的底部对齐。

            【讨论】:

            • 感谢您的帮助。但我的意思是按钮的优先级: 1 - 在列表视图下方,如果它有空间。 2 - 对齐屏幕底部,如果列表视图将其推出一边。
            【解决方案7】:

            使用相对布局。 将按钮放在屏幕底部,并将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>
            

            【讨论】:

            • 没关系。但是当列表视图只有 1 或 2 行时,我希望按钮位于列表视图的正下方。您的解决方案使按钮始终在底部对齐。
            猜你喜欢
            • 2011-01-24
            • 2011-10-14
            • 1970-01-01
            • 1970-01-01
            • 2013-04-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多