【问题标题】:centerVertical property getting lost after updating viewcenterVertical 属性在更新视图后丢失
【发布时间】:2013-09-03 02:32:56
【问题描述】:

在我的布局中,我为我的线性布局之一设置了 centerVertical,该布局内部包含 4 个列表视图。它工作正常。最初每个列表视图作为一个项目。单击任何列表视图中的项目时,我在该列表视图中附加了一个新的数组列表(它就像 1 级菜单和 2 级菜单)。代码工作正常,但是当我使用 2 级数组列表更新任何列表视图时,所有列表视图的 center_Vertical 属性都会丢失,它们会出现在屏幕顶部。

这是我的布局文件:

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


  <LinearLayout
        android:id="@+id/container"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:splitMotionEvents="true">

    <ListView
        android:id="@+id/list_left"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:layout_weight="1"/>

    <ListView
        android:id="@+id/list_center1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:layout_weight="1"/>

    <ListView
        android:id="@+id/list_center2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:scrollbars="none"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:layout_weight="1"/>

    <ListView
        android:id="@+id/list_right"
        android:layout_width="match_parent"
        android:layout_height="match_parent"  
        android:scrollbars="none"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:layout_weight="1"/>
  </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:visibility="gone"
        android:text="Clear Cache"/>
</RelativeLayout>

如果有人可以帮助我,我会非常感激。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    用户j__m's 的建议部分正确:

    Have you tried changing the heights of the ListViews to wrap_content?
    

    用户alex's 的评论也是正确的:

    wrap_content shouldn't be used on ListView.
    

    让我们将建议搁置几分钟,然后实现您的目标:

    在我的布局中,我为我的线性布局之一设置了 centerVertical 其中内部包含 4 个列表视图。它工作正常。

    当然。假设 Linearlayout 的初始高度是 A 个单位(高度是 wrap_content)。而RelativeLayout 的高度是Y 个单位(match_parent => 填满屏幕)。因此,以下内容:

    android:layout_centerVertical="true"
    

    在 y = Y/2 行上方对齐 A/2 个单位的 LinearLayout,在 y = Y/2 下方对齐 A/2 个单位。

    由于所有 ListView 的高度都设置为 match_parent,因此它们的高度也等于 A 单位。这适用于每个列表中的一项。

    现在,考虑 ListView 的高度之一增长到 > Y 个单位的情况。在这种情况下,LinearLayout 的高度也会增长到 Y 个单位。并且:

    android:layout_centerVertical="true"
    

    在 y = Y/2 行上方对齐 Y/2 个单位的 LinearLayout,在 y = Y/2 下方对齐 Y/2 个单位。

    甚至具有一项的 ListViews 从顶部开始。因为,它们的高度也是 Y 单位。

    希望这是有道理的。

    解决办法:

    将每个 ListView 的高度设置为 wrap_content。设置 LinearLayout 的android:gravity="center_vertical"。现在,LinearLayout 有自己的 y= Y/2 轴。高度等于 Y 的 ListViews 将从顶部开始。高度为 A 的 ListView 将保持在 y = Y/2 左右。

    <?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">
    
    
      <LinearLayout
            android:id="@+id/container"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"
            android:splitMotionEvents="true">
    
        <ListView
            android:id="@+id/list_left"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:scrollbars="none"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:layout_weight="1"/>
    
        <ListView
            android:id="@+id/list_center1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:scrollbars="none"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:layout_weight="1"/>
    
        <ListView
            android:id="@+id/list_center2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:scrollbars="none"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:layout_weight="1"/>
    
        <ListView
            android:id="@+id/list_right"
            android:layout_width="0dp"
            android:layout_height="wrap_content"  
            android:scrollbars="none"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:layout_weight="1"/>
      </LinearLayout>
    
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:visibility="gone"
            android:text="Clear Cache"/>
    </RelativeLayout>
    

    另一件事:

    RelativeLayouts 没有方向。

    【讨论】:

    • 精彩.. 很好的解释。它就像魅力:)。非常感谢您的详细解释。
    • @Sushil 完全没问题。有时,获得解决方案并不能像理解问题的原因那样奏效。非常欢迎您!
    • 是的。知道原因更重要:)
    【解决方案2】:
    // you don't need to relative layout try this.
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:splitMotionEvents="true" >
    
        <ListView
            android:id="@+id/list_left"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:scrollbars="none" />
    
        <ListView
            android:id="@+id/list_center1"
              android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:scrollbars="none" />
    
        <ListView
            android:id="@+id/list_center2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:scrollbars="none" />
    
        <ListView
            android:id="@+id/list_right"
             android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:scrollbars="none" />
    </LinearLayout>
    
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Clear Cache"
        android:visibility="gone" />
    
    </LinearLayout>
    

    【讨论】:

    • 感谢您的回答。但是如果我用 LinearLayout 替换 RelativeLayout,“centerVertical”甚至第一次都不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 2021-03-08
    • 2023-04-03
    • 2012-11-04
    相关资源
    最近更新 更多