简体   繁体   中英

Programmatic table row with text and a button

I have this layout which presents my View correctly, I tried to match it with Java side to have the capability to build rows dynamically, but I kept failing into achieving that,

 <TableLayout> 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
               <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

                <LinearLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:orientation="vertical">

                    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextViewaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaws" />
                </LinearLayout>

                <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
            </TableRow>
</TableLayout>

I do want to make it in code, I have tried many times and this is my best attempt

public static void makeTablePostOrders(Context ct, TableLayout tableLayout_PostOrders,
                                   List<String> postOrders)
{
    TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(
        TableRow.LayoutParams.WRAP_CONTENT, 
TableRow.LayoutParams.WRAP_CONTENT);

LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(200,
        LinearLayout.LayoutParams.MATCH_PARENT,1);



//randoms
for(int i = 0; i < postOrders.size()/2 ; i++)
{
    //make tableRow
    TableRow tableRow = new TableRow(ct);
    // make LinearLayout
    LinearLayout layout = new LinearLayout(ct);
    layout.setLayoutParams(linearLayoutParams);
    layout.setOrientation(LinearLayout.VERTICAL);
    //make TextView
    TextView tv = new TextView(ct);
    tv.setText(postOrders.get(i));
    tv.setTextColor(Color.BLACK);
    // make button
    // the button should send us to next view
    Button bt_View = new Button(ct);
    bt_View.setText("Select");

    //adding views
    layout.addView(tv,-2,-2);
    tableRow.addView(layout);
    tableRow.addView(bt_View,-2,-2);
    tableLayout_PostOrders.addView(tableRow,i+1);
}
}

What I want is to make table ROWs with Text and a Button, yet the text should not pass the screen and always go down vertically if needed.

here is a picture, each row will have one of these.

在此处输入图片说明

The correct way to implement the design according to yr requirement is listview or recyclerview. So best to use Listview/RecycleView instead of making list row on fly.

The xml for the list row will look like :

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/myTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".65"
            android:text="You text will come here" />

        <Button
            android:id="@+id/myButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Button" />
    </LinearLayout>

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