【问题标题】:Different column size for a listview in XamarinXamarin 中列表视图的不同列大小
【发布时间】:2017-06-29 22:34:32
【问题描述】:

我有一个列表视图,每一行都在放大以下视图后显示。它是一个 LinearLayout,它工作正常,直到我给每个 textview(这是我列表中的一列)相同的大小。如果我改变任何这些宽度,我会得到意想不到的结果。例如,如果我将 0.5 放在第一个 textView(称为“测试”)上,我希望 textView 的大小要小得多,但事实恰恰相反。所以我尝试使用 3,但又是非常意外的行为。因此,如果我希望第一个 textView 比其他 4 个小得多,我该怎么做呢?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:paddingLeft="5dp"
    android:paddingTop="2dp">
 <TextView
    android:text="Test"
    android:layout_width="match_parent"
    android:layout_weight="3"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="#333333"
    android:id="@+id/testView" />
<TextView
    android:text="Name"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="#333333"
    android:id="@+id/nameView" />
<TextView
    android:text="Surname"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="#333333"
    android:id="@+id/surnameView" />
<TextView
    android:text="Age"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="#333333"
    android:id="@+id/ageView" />
<TextView
    android:text="Profession"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="#333333"
    android:id="@+id/professionView" />

【问题讨论】:

    标签: android xml android-layout listview xamarin


    【解决方案1】:

    如果我改变其中任何一个宽度,我会得到意想不到的结果。

    您得到意外结果的原因是您将每个TextView layout_width 属性设置为match_parentdocument 说:

    子视图可以指定一个权重值,然后视图组中的任何剩余空间按照其声明权重的比例分配给子视图。

    我们可以看到,布局中每个TextViewlayout_widt 属性都应设置为wrap_content。就是TextView的宽度等于原来的宽度(android: layout_width =warp_content)加上剩余空间的比例。就像this

    【讨论】:

    • 非常感谢您的回答。只是有点困惑,所以请原谅我问更多。我的目标是使第一个 textView(名为“Test”)比其他 4 个 textView 小得多。假设第一个 textView 的内容只是 * 或空,所以最多 1 个字符。我希望其他 4 个 textViews 平均填充剩余空间。我应该将 android:layout_height="wrap_content" 用于第一个,将 android:layout_weight="1" 用于其他吗?
    • 可以,删除第一个TextView中的android:layout_weight属性即可。
    • 我希望这可行,但我只能在今晚检查。如果您不介意,我会回复您的答案。但再次感谢您的帮助。
    • 我从第一个 TextView 中删除了 android:layout_weight 属性,但结果仍然错误,第一个 TextView 现在填满了所有空间。然后我意识到我还必须将 android:layout_width 属性从“match_parent”更改为“wrap_content”。现在可以了,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    相关资源
    最近更新 更多