简体   繁体   中英

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. 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?

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 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(1, true);

this does the job!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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