Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am writing an Android app that requires me to programmatically add a TextView to a TableRow which is, in turn, added to a TableLayout. For the design to look proper, the TextView width must be the same as the TableRow width. The problem I am having is that even though I have attempted to set the width of the TextView to match using MATCH_PARENT, it is not taking effect.

Here is a sample of the Java that I am trying to use to do this:

final TableRow myTableRow = new TableRow(getApplicationContext());

final TextView myTextView = new TextView(getApplicationContext());
myTextView.setWidth(TableRow.LayoutParams.MATCH_PARENT);
myTextView.setPadding(5, 0, 5, 0);
myTextView.setTextColor(Color.BLACK);
myTextView.setTextSize(30);

myTextView.setBackgroundColor(Color.RED);

myTextView.setText("1");

myTableRow.addView(myTextView);

myTableLayout.addView(myTableRow); // Assume 'myTableLayout' has already been declared.

Here is the XML for myTableLayout:

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/myTableLayout">
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tableRowOne">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/textViewOne"
            android:layout_weight="1"
            android:textSize="40sp"
            android:paddingStart="5dp"
            android:paddingEnd="5dp" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tableRowTwo">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/textViewTwo"
            android:textColor="#000000"
            android:textSize="30sp"
            android:paddingEnd="5dp"
            android:paddingStart="5dp"
            android:layout_weight="1" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tableRowThree">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textViewThree"
            android:textColor="#000000"
            android:textSize="20sp"
            android:paddingEnd="5dp"
            android:paddingStart="5dp"
            android:layout_weight="1" />
    </TableRow>
</TableLayout>

Does anyone have any idea what I could be doing wrong?

Here is a picture of the way the layout appears. The code posted here is not identical to the actual app code, but it is enough to show the problem. Specifically, the code that was removed concerns the replies. In the example code posted as part of the question, only one TextView is shown in the Java code, but in the actually project, there are two.

Layout Example Image

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
157 views
Welcome To Ask or Share your Answers For Others

1 Answer

There are several ways you can handle this.

  1. TableLayout and TableRow both extends linearlayout, so maybe you need to set Orientation.
  2. you could set the layout params(margin params) like this

    layout_width = 0dp ;
    layout_weight = 1 ;
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...