如何获得动态创建的表格的表格宽度?

时间:2019-03-24 19:50:47

标签: java android dynamic width tablerow

我正在开发用于语言学习的应用程序。我想直接在外语单词下面显示翻译。我目前尝试通过使用动态创建的表来完成此任务。但是表格变宽,然后显示宽度,很多表格都看不到了。我想测试是否会将单词从View中删除,在这种情况下,请继续在下面的新表格中编写这些单词。

如何在运行时获取表格的宽度?

我是android编程的新手。您可能有一个更好的主意,如何在屏幕上显示不同的语言文本?

Resources res  = getResources();
        cn = res.getStringArray(R.array.lesson5_1_HanYu);
        py = res.getStringArray(R.array.lesson5_1_pinyin);
        dt = res.getStringArray(R.array.lesson5_1_deutsch);

        Intent in = getIntent();
        int index = in.getIntExtra("com.example.ITEM_INDEX", -1);

        TableLayout tableLayout;
        TableRow tableRow1, tableRow2, tableRow3;
        TextView cn1TV, py1TV, dt1TV, cn2TV, py2TV, dt2TV, cn3TV, py3TV, dt3TV;
        TextView[] cnTV = new TextView[cn.length];
        TextView[] pyTV = new TextView[py.length];
        TextView[] dtTV = new TextView[dt.length];

        if(index > -1){

            tableLayout = new TableLayout(this);
            tableRow1 = new TableRow(this);
            tableRow2 = new TableRow(this);
            tableRow3 = new TableRow(this);
            TableLayout.LayoutParams lp = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

            int i = 0;
            boolean lineEnd = false;
            while(i < cn.length && lineEnd == false){
                cnTV[i] = new TextView(this);
                cnTV[i].setText(cn[i] + " ");
                pyTV[i] = new TextView(this);
                pyTV[i].setText(py[i] + " ");
                dtTV[i] = new TextView(this);
                dtTV[i].setText(dt[i] + " ");

                tableRow1.addView(cnTV[i]);
                tableRow2.addView(pyTV[i]);
                tableRow3.addView(dtTV[i]);

                // Check if next string is still in DisplayView area:

                // Code ...
                // change value of lineEnd if necessary


                i++; // proceed with next element in next interation
            }

            tableLayout.addView(tableRow1);
            tableLayout.addView(tableRow2);
            tableLayout.addView(tableRow3);

            setContentView(tableLayout);

        }

1 个答案:

答案 0 :(得分:0)

这是我测量文本视图的项目之一的代码示例。您将测量一个TableRow

 TextView filter = findViewById(R.id.list_filter);
           filter.measure(0, 0);

           int center = filter.getLeft() + filter.getMeasuredWidth()/2;

            Point p =  new  ViewTarget(filter).getPoint();




                    Log.i(TAG, "x: " + p.x
                    + " width: " + filter.getWidth()
                    + " right: " + filter.getRight()
                    + " left: " + filter.getLeft()
                    + " measured: " + filter.getMeasuredWidth()
            );

            Log.i(TAG,  "value: " + (0- filter.getLeft() - filter.getMeasuredWidth()));