【问题标题】:Columns in a TableRow divided using weights & weightsums not spacing elements properlyTableRow 中的列使用权重和权重和未正确分隔元素
【发布时间】:2012-02-27 04:06:17
【问题描述】:

我被分配创建一个 Activity,该 Activity 按版本显示对应用所做的更改表。该表有四列,Id、Type、Description 和 Version。

为此,我在滚动视图中放置了一个 TableLayout。然后,我有一个 TableRows 模板,它在运行时动态膨胀。列宽应保持不变;它们可以在高度上溢出,但不能在宽度上溢出。

为了实现这一点,我尝试分别使用列和行容器的 weight & weight: sum 属性。我在两者上都将layout_width 设置为0

问题是列在屏幕边界之外扩展。我会发布图片,但我没有权限。

(风景看起来很近,但你可以看到外边界仍然被切断)。

我注意到,如果孩子们的体重增加了大约 45%,那么权重就接近正确了。

最后,分隔列的视图的权重为零,宽度为 1dip。 (我不确定这是否会导致问题)。

在我看来,权重使用宽度就好像它处于横向模式一样。我是否必须制作两种布局,一种用于纵向,一种用于横向?

我已经为表格行添加了 XML 布局:

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

    <LinearLayout
        android:id="@+id/table_row_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <View
            android:id="@+id/top_border"
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:background="#FFFFFF" />

        <LinearLayout
            android:id="@+id/horizontal_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="4" >

            <View
                android:id="@+id/view1"
                android:layout_width="1dip"
                android:layout_height="match_parent"
                android:background="#FFFFFF" android:layout_weight="0.01"/>

            <TextView
                android:id="@+id/txt_id"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:text="Id" android:textSize="10sp" android:layout_weight="0.5"/>

            <View
                android:id="@+id/view2"
                android:layout_width="1dip"
                android:layout_height="match_parent"
                android:background="#FFFFFF" android:layout_weight="0.01"/>

            <TextView
                android:id="@+id/txt_type"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:text="Type" android:textSize="10sp" android:layout_weight="1"/>

            <View
                android:id="@+id/view3"
                android:layout_width="1dip"
                android:layout_height="match_parent"
                android:background="#FFFFFF" android:layout_weight="0.01"/>

            <TextView
                android:id="@+id/txt_desc"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:text="Description" android:textSize="10sp" android:layout_weight="2"/>

            <View
                android:id="@+id/view4"
                android:layout_width="1dip"
                android:layout_height="match_parent"
                android:background="#FFFFFF" android:layout_weight="0.01"/>

            <TextView
                android:id="@+id/txt_version"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:text="Version" android:textSize="10sp" android:layout_weight="0.5"/>

            <View
                android:id="@+id/view6"
                android:layout_width="1dip"
                android:layout_height="match_parent"
                android:background="#FFFFFF" android:layout_weight="0.01"/>
        </LinearLayout>

        <View
            android:id="@+id/view7"
            android:layout_width="match_parent"
            android:layout_height="1dip" android:background="#FFFFFF"/>

    </LinearLayout>

</TableRow>

上下视图分别是上下边框。

这也是它被添加的表格的布局:

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

    <ScrollView
        android:id="@+id/view_scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:fillViewport="true">

        <TableLayout
            android:id="@+id/table_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:shrinkColumns="0"
            android:stretchColumns="0" >
        </TableLayout>

    </ScrollView>

</LinearLayout> 

【问题讨论】:

  • 您可以添加您的行布局以供参考吗?另外,我会再次检查各个权重的总和是否真的不超过android:weightSum 中指定的值(我假设您在引用此属性时打错了字)。

标签: android android-layout android-tablelayout tablerow android-layout-weight


【解决方案1】:

谢谢 MH。

很抱歉没有尽快回复您。 (我是 Android 和 Stack Overflow 的新手……我没有意识到你回答了我的问题。)我已经添加了行布局。

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

    <LinearLayout
        android:id="@+id/table_row_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <View
            android:id="@+id/top_border"
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:background="#FFFFFF" />

        <LinearLayout
            android:id="@+id/horizontal_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="4" >

            <View
                android:id="@+id/view1"
                android:layout_width="1dip"
                android:layout_height="match_parent"
                android:background="#FFFFFF" android:layout_weight="0.01"/>

            <TextView
                android:id="@+id/txt_id"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:text="Id" android:textSize="10sp"       android:layout_weight="0.20"/>

            <View
                android:id="@+id/view2"
                android:layout_width="1dip"
                android:layout_height="match_parent"
                android:background="#FFFFFF" android:layout_weight="0.01"/>

            <TextView
                android:id="@+id/txt_type"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:text="Type" android:textSize="10sp" android:layout_weight="0.5"/>

            <View
                android:id="@+id/view3"
                android:layout_width="1dip"
                android:layout_height="match_parent"
                android:background="#FFFFFF" android:layout_weight="0.01"/>

            <TextView
                android:id="@+id/txt_desc"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:text="Description" android:textSize="10sp" android:layout_weight="1"/>

            <View
                android:id="@+id/view4"
                android:layout_width="1dip"
                android:layout_height="match_parent"
                android:background="#FFFFFF" android:layout_weight="0.01"/>

            <TextView
                android:id="@+id/txt_version"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:text="Version" android:textSize="10sp" android:layout_weight="0.25"/>

            <View
                android:id="@+id/view6"
                android:layout_width="1dip"
                android:layout_height="match_parent"
                android:background="#FFFFFF" android:layout_weight="0.01"/>
        </LinearLayout>

        <View
            android:id="@+id/view7"
            android:layout_width="match_parent"
            android:layout_height="1dip" android:background="#FFFFFF"/>

    </LinearLayout>

</TableRow>

大卫

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 2023-03-29
    • 2020-08-28
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    相关资源
    最近更新 更多