android: two issues using Tablerow+TextView in Tablelayout

后端 未结 3 1659
生来不讨喜
生来不讨喜 2020-12-16 03:46

I am using Tablerow+TextView to make a simple view for blog posts and their replies. In each TableRow I put a TextView in. Now I have two issues:

  1. The text w

相关标签:
3条回答
  • 2020-12-16 04:06

    A more appropriate thing to do for wrapping items would have been to add android:shrinkColumns="*" or android:shrinkColumns="1" to the TableLayout, this would probably have fixed the wrapping issue.

    For Details

    0 讨论(0)
  • 2020-12-16 04:09

    This isn't really a complete answer, but it really seems like you're doing this the hard way.

    Instead of constructing your TableRows manually, you should set them up in xml like this:

    tablerow.xml:

    <TableRow xmlns:android="http://schemas.android.com/apk/res/android">
        <TextView android:id="@+id/content"
            android:singleLine="false"
            android:textAppearance="@style/someappearance" />
    </TableRow>
    

    Prior to your loop, get a reference to a LayoutInflater:

    LayoutInflater inflater = getLayoutInflater();
    

    Then, inside your loop, create an instance of tablerow using the LayoutInflater:

    TableRow row = (TableRow)inflater.inflate(R.layout.tablerow, tl, false);
    TextView content = (TextView)row.findViewById(R.id.content);
    content.setText("this is the content");
    
    tl.addView(row);
    

    This will allow you to set your layout, appearance, layout params in xml making it much easier to read and debug.

    For the scrolling problem, you'll need to add your TableLayout to a ScrollView. Something like this in your xml:

    <ScrollView>
        <TableLayout android:id="@+id/arcitle_content_table" />
    </ScrollView>
    
    0 讨论(0)
  • 2020-12-16 04:20

    To wrap text in table rows:

    By default, TableLayout rows fit the width of their content, no matter it goes over the screen bounds. To get the wider-than-screen text cells to wrap to multi-line, use android:shrinkColumns attribute on TableLayout.

    <TableLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:shrinkColumns="*" />
    

    android:shrinkColumns is zero-based index of the columns to shrink. It removes unnecessary extra space from a column and shrinks it :

    • android:shrinkColumns="*" shrinks all columns
    • android:shrinkColumns="0" shrinks first column
    • android:shrinkColumns="1,2" shrinks the second and third columns

    android:stretchColumns does the opposite. It stretches a column to the maximum available width.

    Both "shrink" and "stretch" consider all rows of the table to compute space.

    To scroll down a TableLayout:

    If your TableLayout is higher than the screen, move it in a ScrollView.

    0 讨论(0)
提交回复
热议问题