Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

android - TextViews do not Stretch to MATCH_PARENT

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

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

1 Reply

0 votes
by (71.8m points)

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 ;
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...