【问题标题】:How to make android:layout_gravity="bottom|center_horizontal" stays at bottom如何使 android:layout_gravity="bottom|center_horizo​​ntal" 停留在底部
【发布时间】:2012-05-02 13:28:36
【问题描述】:

我有一个垂直的 LinearLayout,它有一个列表视图,底部有一个 ImageView,顶部有一个,列表视图填满了留下的空间。

看起来像这样:

<LinearLayout layout_height="match_parent" layout_width="@dimen/width"
    orientation="vertical"> 
    <ImageView layout_height="@dimen/width" layout_width="@dimen/width" id="@+id/top">

     <ListView  android:layout_height="0px"
        android:background="#00ffff"
        android:layout_width="@dimen/width"
        android:layout_weight="1" />

     <ImageView layout_height="@dimen/width" layout_width="@dimen/width" id="@+id/bottom" layout_gravity="bottom|center_horizontal">

只要列表视图可见,它就可以正常工作。

但是,每当我将 ListView 设置为可见性消失时,“底部”ImageView 就会弹出到顶部 Image View 的正下方。

我的问题是为什么底部 ImageView 不停留在底部,尽管我说过'android:layout_gravity="bottom|center_horizo​​ntal"' 我查看了 hierarchyViewer,父级的高度确实与屏幕的高度匹配。所以底部图像视图应该尊重 android:layout_gravity="bottom|center_horizo​​ntal" 并保持在底部,对吧?

【问题讨论】:

  • 您是否要求ListViewgone,还是可以将其设置为invisble?我很确定后者会给你你想要的东西。

标签: android android-layout layout-gravity


【解决方案1】:

最好的解决方案是为此使用 ralaticelayout::::

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#0000FF">

 <ImageView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/top" />
<ImageView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/bottom" android:layout_alignParentBottom="true"/>
     <ListView  android:layout_height="fill_parent"
        android:background="#00ffff"
        android:layout_width="fill_parent"
        android:layout_below:@id/top android:layout_above:@id/bottom />


</RelativeLayout>

【讨论】:

  • 很抱歉,您的建议毫无意义。带有orientation 属性的RelativeLayout?和一个有体重的儿童观点?这实际上将始终以0 的高度呈现ListView - 换句话说,它根本不可见。最后,ListView 和底部ImageView 将重叠,这可能不是 OP 所追求的......
  • 移除了方向,不需要相对布局。而0dp表示占用剩余空间。
  • 你真的尝试过你的建议吗?据我所知,RelativeLayout 中忽略了权重,这意味着它的固定高度为零...
  • 是的,我们只是减轻重量。对此感到抱歉。从帖子中删除它
【解决方案2】:

作为我的 cmets 的替代方法,您可以将 LinearLayout 替换为 RelativeLayout。 Agarwal 已经提出了这个建议,但是他的代码有点乱,在撰写本文时,就您希望它执行的操作而言,它并不正确。

试试下面的代码。当您将ListView 设置为gone 时,顶部和底部图像将保持与visible 相同的位置。请注意,我替换了 @dimens/width 引用,因此您可能希望将它们放回原处。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <ImageView android:id="@+id/top" android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_above="@+id/bottom"
        android:layout_below="@+id/top" android:background="#00ffff" />

    <ImageView android:id="@+id/bottom" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

【讨论】:

  • 谢谢。是否可以使 ListView 从底部而不是从顶部“增长”。即第一个元素显示在 ListView 的底部,然后第二个元素显示在第一个元素的上方,以此类推?
  • @michael:如果我没记错的话,您可以为此目的使用stackFromBottom 标志。通过 xml 设置它:android:stackFromBottom,或代码:setStackFromBottom(boolean)“当 stack from bottom 设置为 true 时,列表从视图底部开始填充其内容。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-10
  • 2022-11-27
  • 2010-11-01
  • 2015-01-28
相关资源
最近更新 更多