简体   繁体   English

文本视图在表格布局中走出屏幕

[英]Textview going out of screen in tablelayout

I am new to android and I have used a TableLayout and I am adding two columns of data into it from a string array.我是 android 新手,我使用了 TableLayout,我从字符串数组中向其中添加了两列数据。 But my second column goes out of screen.但是我的第二列从屏幕上消失了。 It seems that all entries of my second column start from where the longest entry in the first column ends.似乎我的第二列的所有条目都从第一列中最长条目结束的地方开始。 How to make both the columns fit into the screen with like 50% width for both columns?如何使两列都适合屏幕,两列的宽度都为 50%?

My code goes like this..我的代码是这样的..

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.findia1);

        TableLayout tl = (TableLayout) findViewById(R.id.tablay1);

        for (int i = 0; i < s1.length; i += 2) {
            TableRow tr1 = new TableRow(this);
            tr1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));

            TextView textview1 = new TextView(this);
            textview1.setText(s1[i]);
            tr1.addView(textview1);

            TextView textview2 = new TextView(this);
            textview2.setText(s1[i+1]);
            tr1.addView(textview2);

            tl.addView(tr1, new TableLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));


        }

xml code is xml代码是

<?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="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/tablay1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         >

    </TableLayout>

</LinearLayout>

i guess i got the answer.. i used我想我得到了答案..我用过

tl.setColumnShrinkable(0, true); tl.setColumnShrinkable(0, true); tl.setColumnShrinkable(1, true); tl.setColumnShrinkable(1, true);

this does the job!这样就可以了!

为 TextView 赋予权重android:layout_weight="1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM