【问题标题】:Android custom component width not matching parentAndroid自定义组件宽度不匹配父
【发布时间】:2013-10-25 12:55:39
【问题描述】:

我有一个简单的组件 - 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 小时..:/ 我想这是我所缺少的非常小的和愚蠢的东西。感谢您的所有想法。

【问题讨论】:

  • 你正在使用tablelayout,那么你的第一个tablerow的宽度是多少
  • 表格布局使用 match_parent 以及宽度和高度..
  • 它是你的第一个表格行,cz 在表格布局中,第一个 tablr 行的属性被所有定义的行使用
  • 好的问题解决了..这不完全是第一个表格行的问题..问题是自己的表格行..当我删除它时..不再出现问题并且信息面板正在显示..但是感谢您的提示..对我有所帮助..如果您想写一个答案,我会标记它,因为它在您的评论中:]

标签: android layout width android-linearlayout custom-component


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多