0

我给。

我正在尝试扩展 TableLayout 类(只是这样我就可以覆盖 onMeasure )并且我正在扩展一个在 XML 中定义的 TableLayout。

TableLayout 出现了(我可以看到它,由于它的自定义背景),但 TableRows 没有。

即使在 Eclipse 的 XML“图形布局”中,如果我强制 TableLayout 成为我的自定义扩展类(com.example.MainApplication.TableLayout_Schedrunning),嵌套的 TableRows 也不存在。

但是,如果我将 XML 更改为 TableLayout,TableRows 就会神奇地出现。

<?xml version="1.0" encoding="utf-8"?>
<!-- this doesn't work-->
<com.example.MainApplication.Tablelayout_Schedrunning xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frmlay_schedule"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:padding="2dp"
android:background="@drawable/schedrunning_background" >
<TableRow 
    android:id="@+id/tblrow_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="5dp"
    >
    <TextView 
        android:id="@+id/txtSchedRunningHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Schedule Running"
        android:textColor="@android:color/white"
        android:textSize="13dp"/>

    <TextView 
        android:id="@+id/txtCurrScheduleRunning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="11dp"/>
</TableRow>

</com.example.MainApplication.Tablelayout_Schedrunning>

再次重申——如果我将上面的自定义类更改为 simple <TableLayout>,TableRow 中的嵌套 TextViews 就会出现。如果我不这样做,他们就不会。我错过了什么简单的事情?

哦,TableLayout 是一个非常标准的扩展:

public class Tablelayout_Schedrunning extends TableLayout {

public Tablelayout_Schedrunning(Context context) {
    super(context);
    Log.w("tablelayout","simple context");
    // TODO Auto-generated constructor stub
}

public Tablelayout_Schedrunning(Context context, AttributeSet attrs) {
    super(context, attrs);
    Log.w("tablelayout","advanced, context and attrs");
    // TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);

    Log.w("tablelayout","parentWidth is " + parentWidth);
    Log.w("tablelayout","parentHeight is " + parentHeight);
    //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(parentWidth, (int) (parentHeight * .4));
}
4

1 回答 1

1

我认为你应该在你的扩展中添加super.onMeasuresetLayoutParamsView

于 2012-09-07T01:54:56.340 回答