【问题标题】:Android ListView width keeps changingAndroid ListView 宽度不断变化
【发布时间】:2017-07-07 21:02:17
【问题描述】:

首先,我是 android 新手,所以如果我错过了一些基本的东西,我深表歉意。

我有一个页面,它有两个并排的 ListView,两个都有不同数量的内容。我在 ListViews 上方还有一个 TextView,在 listviews 下方还有另一个 TextView。这些文本视图框会根据在两个 ListView 中的任何一个中选择的项目而变化。

这两个 ListView 并排放置,每个都占据屏幕的一半,而 Textview 位于正上方和正下方,均以页面为中心。如下图所示。

This is the page looking normal on load.

问题是当我从任一列表中选择一个项目时。我有一种感觉,我缺少一些 XML 属性,但我不确定是哪些属性,或者是否是这种情况。当一个项目被选中时,假设从右侧的 ListView 中,底部的 TextView 将使用从数组中获取的文本进行更新。 ListView 也决定改变宽度,我不知道为什么会这样......我不希望 ListView 改变宽度。我希望它继续占据页面的一半和页面的一半。

This is the page after an item from the right ListView has been selected.

我也想把东西保存在RelativeLayout中。我也相信这只是一个 XML 问题,与适配器或任何其他代码无关,所以我暂时不包括在内。如果需要,我可以包含它。

任何帮助都会很棒。提前致谢。

content_titles.xml我的活动 xml 文件

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TitlesActivity">

<ListView
    android:id="@+id/unlocked_titles_list"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:longClickable="true"
    android:layout_below="@+id/current_title"
    android:layout_alignEnd="@+id/requirements"
    android:layout_marginEnd="26dp"
    android:layout_above="@+id/requirements">
</ListView>

<TextView
    android:id="@+id/current_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="65dp"
    android:text="Current Title: Novice"
    android:textSize="24sp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"/>

<ListView
    android:id="@+id/locked_titles_list"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignStart="@+id/requirements"
    android:layout_marginStart="28dp"
    android:layout_above="@+id/requirements"
    android:layout_below="@+id/current_title"/>

<TextView
    android:id="@+id/requirements"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="34dp"
    android:text="temp"
    android:textAlignment="center"
    android:textSize="24sp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>

activity_listview.xml 用作 ListView 的各个行

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/label"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:padding="10dip"
      android:textSize="16dip"
      android:textStyle="bold" >
</TextView>

【问题讨论】:

    标签: android xml listview width


    【解决方案1】:

    布局的问题可能是因为 ScrollView 是包装器

    我偶然发现了http://developer.android.com/reference/android/widget/ExpandableListView.html中的一些注释

    "...注意:如果父级的大小也没有严格指定(例如,如果父级是 ScrollView,则不能指定 wrap_content,因为 XML 中 ExpandableListView 的 android:layout_height 属性它也可以是任意长度。但是,如果 ExpandableListView 父级具有特定大小,例如 100 像素,则可以使用 wrap_content。"

    我删除了 ScrollView 的包装,线性布局开始正常工作。现在它只是了解如何将这些东西包装到 ScrollView。上帝帮助我

    但无论如何,这确实是一种奇怪的行为。我认为 fill_parent 的措辞并不正确。使用 heirarchyviewer 工具时,我总是看到 layout_width 和 leayout_height 的 WRAP_CONTENT 和 MATCH_PARENT 值。所以可能 fill_parent 实际上意味着 match_parent 这让我陷入认知失调。

    【讨论】:

      【解决方案2】:

      您已将layout_width 属性设置为wrap_content。这意味着它们可以随着数据的变化而变化。我建议将您的ListViews 放在带有orientation:horizontalLinearLayout 中,并使用layout_weight 设置每个元素占用的空间量。这是一个相关的 SO 问题What does android:layout_weight mean?

      【讨论】:

      • 我真的很想把它保留在RelativeLayout中。还有其他方法吗?
      猜你喜欢
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      相关资源
      最近更新 更多