0

我有一个简单的组件——InfoPanel 扩展了 LinearLayout,里面有 3 个另外的线性布局。这 3 个线性布局中的每一个都包含 2 个文本视图。

<?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="wrap_content"
    android:background="#F0F"
    android:orientation="horizontal"
    android:baselineAligned="false" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="0.3"
        android:background="@android:color/transparent"
        android:gravity="left|center_vertical" >

                 <TextView
                     android:layout_width="0dp"
                     android:layout_height="wrap_content"
                     android:layout_weight="0.5"
                     android:background="@android:color/transparent"
                     android:gravity="right|center_vertical"
                     android:text="@string/info_lineM" />

                 <TextView
                    android:id="@+id/info_lineM"
                     android:layout_width="0dp"
                     android:layout_height="wrap_content"
                     android:layout_weight="0.5"
                     android:background="@android:color/transparent"
                     android:gravity="left|center_vertical"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="0.4"
        android:gravity="center_horizontal|center_vertical"
                 android:background="@android:color/transparent">

                 <TextView
                     android:layout_width="0dp"
                     android:layout_height="wrap_content"
                     android:layout_weight="0.5"
                     android:background="@android:color/transparent"
                     android:gravity="right|center_vertical"
                     android:text="@string/info_lineC" />

                 <TextView
                    android:id="@+id/info_lineC"
                     android:layout_width="0dp"
                     android:layout_height="wrap_content"
                     android:layout_weight="0.5"
                     android:background="@android:color/transparent"
                     android:gravity="left|center_vertical" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="0.3"
        android:gravity="right|center_vertical"
                 android:background="@android:color/transparent">

                 <TextView
                     android:layout_width="0dp"
                     android:layout_height="wrap_content"
                     android:layout_weight="0.5"
                     android:background="@android:color/transparent"
                     android:gravity="right|center_vertical"
                     android:text="@string/info_lineG" />

                 <TextView
                    android:id="@+id/info_lineG"
                     android:layout_width="0dp"
                     android:layout_height="wrap_content"
                     android:layout_weight="0.5"
                     android:background="@android:color/transparent"
                     android:gravity="left|center_vertical" />
    </LinearLayout>
</LinearLayout>

这个组件的 Java 类非常简单:

public class InfoPanel extends LinearLayout{
public InfoPanel(Context ctx)
    {
        super(ctx);
        LayoutInflater.from(ctx).inflate(R.layout.info_panel, this);
    }

    public InfoPanel(Context ctx, AttributeSet attr)
    {
        super(ctx,attr);
        LayoutInflater.from(ctx).inflate(R.layout.info_panel, this);
    }
    @Override
    public void onFinishInflate()
    {
        super.onFinishInflate();
    }
}

我使用该组件的主要 XML 看起来像:

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <com.project.my.components.InfoPanel
        android:id="@+id/InfoPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</TableRow>

问题是,我的组件的宽度只有屏幕宽度的 10% 左右。现在我正在尝试搜索一些相同的问题(我发现了很多主题,但对我没有任何帮助)超过 5 小时..:/ 我想这是我所缺少的非常小的和愚蠢的东西。感谢所有的想法。

4

1 回答 1

1

很好,您找到了问题的解决方案,表格行具有使用所有行中第一行属性的默认行为。但删除它解决了您的问题,这是关键。

于 2013-10-25T04:55:39.830 回答