Android首次的TableRow没有显示 [英] Android first TableRow is not showing

查看:283
本文介绍了Android首次的TableRow没有显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题。我得出MessageRow从的TableRow。我填充一个表与一个的TableRow(头)和100 MessageRows。但是,当我的表添加到Horizo​​ntalScroll视图的只有Messagerows都出现
如果我检查表中的调试器,所有的行都是存在的,包括头部,用正确的儿童和文字。

I have a weird problem. I derived MessageRow from TableRow. I populated a table with one TableRow (Header) and 100 MessageRows. But when I add the table to the HorizontalScroll view only the Messagerows are showing. If I inspect the table in the debugger, all the rows are there, including the Header, with the right children and text.

这是简化的code:

public class MessageRow extends TableRow {
    public TextView tvData1;
    public TextView tvData2;
    public TextView tvData3;

    public MessageRow(Context context) {
        this(context, null);
    }

    public MessageRow(Context context, AttributeSet attrs) {
        super(context, attrs);
        tvData1 = new TextView(context);
        tvData2 = new TextView(context);
        tvData3 = new TextView(context);
    }

    public void setData(String data1, String data2, String data3) {
        tvData1.setText(data1);
        tvData2.setText(data2);
        tvData3.setText(data3);

        addView(tvData1);
        addView(tvData2);
        addView(tvData3);
    }
}

活动code:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewer);

        mTable = new TableLayout(this);

        HorizontalScrollView hview = (HorizontalScrollView) findViewById(R.id.hscroll);
        mTable.setBackgroundColor(Color.WHITE);
        populate(mTable);
        hview.addView(mTable);
    }


    public void setHeader(TableLayout tl) {
        TableRow mHeader = new TableRow(getContext());
        mHeader.addView(getColumnHeader("Data#1"));
        mHeader.addView(getColumnHeader("Data#2"));
        mHeader.addView(getColumnHeader("Data#3"));
        mHeader.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        tl.addView(mHeader);
    }

    public void populate(TableLayout tl) {
        setHeader(tl);
        for(int i = 0; i < 100; i++) {
            MessageRow mr = new MessageRow(getContext());
            mr.setMessage("xxx"+i,"yyy"+i,"zzz"+i);
            mr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            tl.addView(mr);
        }
    }

    private Button getColumnHeader(String name) {
        Button bt = new Button(getContext());
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.rightMargin = params.leftMargin = 2;
        bt.setLayoutParams(params);
        bt.setText(name);
        return bt;
    }

搜索结果
现在,我想的旁路的在 getColumnHeader()的功能是这样的:

    public void setHeader(TableLayout tl) {
        TableRow mHeader = new TableRow(getContext());
        //mHeader.addView(getColumnHeader("Data#1"));
-->         Button bt1 = new Button(getContext());
-->         bt1.setText("Data#1");
-->         mHeader.addView(bt1);
        //mHeader.addView(getColumnHeader("Data#2"));
-->         Button bt2 = getColumnHeader("Data#2");
-->         mHeader.addView(bt2);

        mHeader.addView(getColumnHeader("Data#3"));
        mHeader.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        tl.addView(mHeader);
    }

,然后显示BT1(数据#1)!但数据#2或数据#3。这暗示,在某种程度上功能的 getColumnHeader()是坏的。但不是acording调试器,当我的检查 BT1和BT2他们似乎不错,我无法检测是手动实例化按钮BT1和返回按钮BT2的区别

and then bt1 (Data#1) is displayed!!! but not Data#2 or Data#3. This hints that somehow the function getColumnHeader() is bad. But not acording to the debugger, when I inspect bt1 and bt2 they seem good, I cannot detect what is the difference between the manually instantiated button bt1 and the returned button bt2.

附注:我不知道这是不是最好的办法,但对两个变量比较我只需右键点击,复制变量,然后将其粘贴到两个不同的文件记事本+ +(一个用于BT1和一个BT2)和使用的比较插件。

推荐答案

最后一块拼图。结果
我尝试了很多的变化,最后它的工作!结果
更换的LayoutParams TableRow.LayoutParams 的伎俩。结果
它们的结构非常相似,甚至可能TableRow.LayoutParams是从的LayoutParams的。结果
我会离开这里的奥秘,直到一些专家揭示了为什么会这样点亮。搜索结果
最后code:

Last piece of the puzzle.
I tried many variations and finally it worked!
Replacing LayoutParams with TableRow.LayoutParams did the trick.
Their structure is very similar, probably even TableRow.LayoutParams is derived from LayoutParams.
I will leave the mystery here, until some expert sheds light on why this happened.

The final code:

private Button getColumnHeader(String name) {
    Button bt = new Button(getContext());
    TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT);
    params.rightMargin = params.leftMargin = 2;
    bt.setLayoutParams(params);
    bt.setText(name);
    return bt;
}

这篇关于Android首次的TableRow没有显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆