0

我尝试使用这个表格布局: http: //blog.stylingandroid.com/archives/432 并遇到了一些麻烦。我一直在寻找原因很长时间,我决定更聪明地问:D

我的表代码:

package com.stylingandroid.ScrollingTable;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;

public class ScrollingTable extends LinearLayout
{
    public ScrollingTable( Context context )
    {
        super( context );
    }

    public ScrollingTable( Context context, AttributeSet attrs )
    {
        super( context, attrs );
    }

    @Override
    protected void onLayout( boolean changed, int l, int t, int r, int b )
    {
        super.onLayout( changed, l, t, r, b );
        List<Integer> colWidths = new LinkedList<Integer>();

        final TableLayout header = (TableLayout) findViewById( R.id.HeaderTable );
        TableLayout body = (TableLayout) findViewById( R.id.BodyTable );

        for ( int rownum = 0; rownum < body.getChildCount(); rownum++ )
        {
            TableRow row = (TableRow) body.getChildAt( rownum );
            for ( int cellnum = 0; cellnum < row.getChildCount(); cellnum++ )
            {
                View cell = row.getChildAt( cellnum );
                Integer cellWidth = cell.getWidth();
                if ( colWidths.size() <= cellnum )
                {
                    colWidths.add( cellWidth );
                }
                else
                {
                    Integer current = colWidths.get( cellnum );
                    if( cellWidth > current )
                    {
                        colWidths.remove( cellnum );
                        colWidths.add( cellnum, cellWidth );
                    }
                }
            }
        }

        for ( int rownum = 0; rownum < header.getChildCount(); rownum++ )
        {

            final TableRow row = (TableRow) header.getChildAt(rownum);
            for (int cellnum = 0; cellnum < row.getChildCount(); cellnum++ ) {
                View cell = row.getChildAt( cellnum );
                TableRow.LayoutParams params = (TableRow.LayoutParams)cell.getLayoutParams();
                params.width = 200;
                cell.setOnClickListener(new OnClickListener(){
                    public void onClick(View v){
                        int wid = v.getWidth();
                         Log.i("crap", "wid" + wid);
                    }
                });

            }
        }
    }
}

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.stylingandroid.ScrollingTable.ScrollingTable
        android:layout_width="fill_parent"
        android:orientation="vertical" android:layout_weight="1"
        android:layout_height="fill_parent">
        <TableLayout android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:id="@+id/HeaderTable">
            <TableRow style="@style/HeaderRow">
                <TextView style="@style/HeaderText" android:text="Col 1" />
                <TextView style="@style/HeaderText" android:layout_weight="1"
                    android:text="Col 2" />
<!--                <TextView style="@style/HeaderText" android:text="Col 3" android:layout_span="2" / -->
<!--                TextView style="@style/HeaderText" android:text="Col 4" / -->
                <TextView style="@style/HeaderText" android:text="Col 333" />
                <TextView style="@style/HeaderText" android:text="Col 4" />
            </TableRow>
            <TableRow style="@style/BodyRow">
                <TextView android:text="Dummy" style="@style/BodyText" android:height="0dp" />
                <TextView android:text="Dummy" style="@style/BodyText"
                    android:layout_weight="1" android:height="0dp" />
                <TextView android:text="Dummy" style="@style/BodyText" android:height="0dp" />
                <TextView android:text="Dummy" style="@style/BodyText" android:height="0dp" />
            </TableRow>
        </TableLayout>
        <ScrollView android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TableLayout android:layout_height="wrap_content"
                android:layout_width="fill_parent" android:id="@+id/BodyTable">
                <TableRow style="@style/BodyRow">
                    <TextView android:text="Cell 1,1" style="@style/BodyText" />
                    <TextView android:text="Cell 1,2" style="@style/BodyText"
                        android:layout_weight="1"/>
                    <TextView android:text="Cell 1,3" style="@style/BodyText"/>
                    <TextView android:text="Cell 1,4" style="@style/BodyText" />
                </TableRow>
                <TableRow style="@style/BodyRow">
                    <TextView android:text="Cell 2,1" style="@style/BodyText" />
                    <TextView android:text="Cell 2,2" style="@style/BodyText"
                        android:layout_weight="1" />
<!--                    <TextView android:text="Cell 2,3" style="@style/BodyText" android:layout_span="2"/> -->
<!--                    TextView android:text="Cell 2,4" style="@style/BodyText" / -->
                    <TextView android:text="Cell 2,3" style="@style/BodyText" />
                    <TextView android:text="Cell 2,4" style="@style/BodyText" />                
                </TableRow>
                <TableRow style="@style/BodyRow">
                    <TextView android:text="Cell 3,1" style="@style/BodyText" />
                    <TextView android:text="Cell 3,2" style="@style/BodyText"
                        android:layout_weight="1" />
                    <TextView android:text="Cell 3,3" style="@style/BodyText" />
                    <TextView android:text="Cell 3,4" style="@style/BodyText" />
                </TableRow>
                <TableRow style="@style/BodyRow">
                    <TextView android:text="Cell 4,1" style="@style/BodyText" />
                    <TextView android:text="Cell 4,2" style="@style/BodyText"
                        android:layout_weight="1" />
                    <TextView android:text="Cell 4,3" style="@style/BodyText" />
                    <TextView android:text="Cell 4,4" style="@style/BodyText" />
                </TableRow>
                <TableRow style="@style/BodyRow">
                    <TextView android:text="Cell 5,1" style="@style/BodyText" />
                    <TextView android:text="Cell 5,2" style="@style/BodyText"
                        android:layout_weight="1" />
                    <TextView android:text="Cell 5,3" style="@style/BodyText" />
                    <TextView android:text="Cell 5,4" style="@style/BodyText" />
                </TableRow>
                <TableRow style="@style/BodyRow">
                    <TextView android:text="Cell 6,1" style="@style/BodyText" />
                    <TextView android:text="Cell 6,2" style="@style/BodyText"
                        android:layout_weight="1" />
                    <TextView android:text="Cell 6,3" style="@style/BodyText" />
                    <TextView android:text="Cell 6,4" style="@style/BodyText" />
                </TableRow>
                <TableRow style="@style/BodyRow">
                    <TextView android:text="Cell 7,1" style="@style/BodyText" />
                    <TextView android:text="Cell 7,2" style="@style/BodyText"
                        android:layout_weight="1" />
                    <TextView android:text="Cell 7,3" style="@style/BodyText" />
                    <TextView android:text="Cell 7,4" style="@style/BodyText" />
                </TableRow>
                <TableRow style="@style/BodyRow">
                    <TextView android:text="Cell 8,1" style="@style/BodyText" />
                    <TextView android:text="Cell 8,2" style="@style/BodyText"
                        android:layout_weight="1" />
                    <TextView android:text="Cell 8,3" style="@style/BodyText" />
                    <TextView android:text="Cell 8,4" style="@style/BodyText" />
                </TableRow>
            </TableLayout>
        </ScrollView>
    </com.stylingandroid.ScrollingTable.ScrollingTable>
</LinearLayout>

问题是这样的:我将宽度设置为这样的固定值: ... params.width = 200;

但是当我点击标题时,值只是 200 WTH?

4

0 回答 0