ListView控件对Android的极限高度 [英] Limit height of ListView on Android

查看:241
本文介绍了ListView控件对Android的极限高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示下一个ListView的按钮。问题是,如果在ListView被延长(添加的项目...),按钮被排挤出画面。

I want to show a button under a ListView. Problem is, if the ListView gets extended (items added...), the button is pushed out of the screen.

我试图与权重的LinearLayout(如<一个建议href="http://stackoverflow.com/questions/4054567/android-why-is-there-no-maxheight-for-a-view">Android:为什么没有对了maxHeight一个视图?),但无论是我得到的权重错了,或者根本没有工作。

I tried a LinearLayout with weights (as suggested in Android: why is there no maxHeight for a View?), but either I got the weights wrong or it simply didn't work.

另外,我发现在某处使用RelativeLayout的提示。 ListView控件然后将用上面的按钮设置安卓layout_above参数。

Also, I found somewhere the hint to use a RelativeLayout. The ListView would then be set above the button with the "android:layout_above" param.

这个问题是,我不知道该怎么算账定位按钮。在我发现的例子,ListView控件下方查看使用调整机器人:layout_alignParentBottom,但我不希望我的按钮,附着在屏幕的底部。

Problem with this is that I don't know how to position the button afterwards. In the example I found, the View below the ListView was adjusted using "android:layout_alignParentBottom", but I don't want my button to cling to the bottom of the screen.

除了采用自动调用setHeight,方法和所需空间的一些计算任何想法?

Any ideas apart from using setHeight-method and some calculating of the required space?

感谢您的时间, 水母

编辑: 我得到了很多有用的答案,感谢大家!

  • bigstone的&放大器; user639183的解决方案的几乎的完美。然而,我必须的额外填充/余量添加到按钮的底部,因为它仍然会被推一半出路屏幕(但然后停止)

  • bigstone's & user639183's solution almost worked perfectly. However, I had to add an extra padding/margin to the bottom of the button, as it still would be pushed half way out of the screen (but then stopped)

如果你想固定在屏幕底部的按钮Adinia的相对布局的答案只能是罚款。这不是我的本意,但仍可能对他人有用的。

Adinia's answer with the relative layout only is fine if you want the button fixed to the bottom of the screen. It's not what I intended but still might be useful for others.

ANGELOS的解决方案是一个我选择在年底,因为它只是创造我想要的效果。但是,我做了两个小的改动的LinearLayout周围的按钮:

AngeloS's solution was the one I chose at the end as it just created the effects I wanted. However, I made two minor changes to the LinearLayout around the button:

  • 首先,因为我不想在我的布局任何绝对价值,我改变了机器人:layout_height =45像素到WRAP_CONTENT,其工作也很不错。

  • First, as I didn't want to have any absolute values in my layout, I changed android:layout_height="45px" to "wrap_content", which works just fine as well.

二,因为我想按钮被horizo​​ntically中心,它仅支持垂直的LinearLayout,我改变了机器人:方向=横向,以垂直。

Second, as I wanted the button to be centered horizontically, which is only supported by vertical LinearLayout, I changed android:orientation="horizontal" to "vertical".

安吉洛还表示在他最初的职位,他不知道,如果机器人:layout_weight =0.1参数周围的ListView中的LinearLayout中有任何影响;我只是想,它实际上呢!如果没有,该按钮被排挤出画面了。

AngeloS also stated in his initial post that he was not sure if the android:layout_weight="0.1" param in the LinearLayout around the ListView had any effect; I just tried and it actually does! Without, the button gets pushed out of the screen again.

推荐答案

我有这个确切的问题,并解决它,我创建了两个单独的 LinearLayouts 来容纳的ListView 按钮分别。从那里,我把两个在另一个线性布局,并设置容器的安卓方向垂直。我还设置了的LinearLayout ,里面的的ListView 0.1 ,但我不知道是否有任何影响。从那里,你可以设置在底部容器的高度(有你的按钮),你就什么高度一样。

I had this exact issue and to solve it I created two seperate LinearLayouts to house the ListView and Button respectively. From there I put both in another Linear Layout and set that container's android:orientation to vertical. I also set the weight of the the LinearLayout that housed the ListView to 0.1 but I dont know if that has any effect. From there, you can set the height of the bottom container (that has your button) to whatever height you would like.

修改这就是我的意思是:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">

  <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        >   



                  <ListView android:id="@+id/ListView01" 
                    android:layout_height="fill_parent"
                    android:layout_width="fill_parent"
                    android:dividerHeight="2px">
                 </ListView>



    </LinearLayout> 

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="45px"
    android:background="@drawable/drawable" 
    > 


                <Button android:background="@drawable/btn_more2"                     
                     android:id="@+id/moreButton"
                     android:layout_width="wrap_content"
                     android:layout_height="fill_parent"
                     android:layout_alignParentRight="true"
                     android:paddingRight="20px"
                     />

</LinearLayout>
</LinearLayout>

上面的溶液将修复按钮在屏幕的底部。

The above solution will fix the button the the bottom of the screen.

要具备按钮浮在列表的底部,改变 ListView01 的高度 WRAP_CONTENT

To have the button float at the bottom of the list, change the height of ListView01 to wrap_content:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical">

  <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        >   

        <ListView android:id="@+id/ListView01" 
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"
                    android:dividerHeight="2px">
                 </ListView>



    </LinearLayout> 

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="45px"
    android:background="@drawable/drawable" 
    > 


                <Button android:background="@drawable/btn_more2"                     
                     android:id="@+id/moreButton"
                     android:layout_width="wrap_content"
                     android:layout_height="fill_parent"
                     android:layout_alignParentRight="true"
                     android:paddingRight="20px"
                     />

</LinearLayout>
</LinearLayout>

这篇关于ListView控件对Android的极限高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆