TextViews do not Stretch to MATCH_PARENT

限于喜欢 提交于 2020-01-07 08:07:31

问题


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


回答1:


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 ;
    



回答2:


The best way to give height and width property to a view is to use Layout Parameters. most of times (almost all the times for me) setting width and height directly wont work.

TableRow.LayoutParams mParam=new TableRow.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);

// set layout parameter to view
textView.setLayoutParams(mParam);            


来源:https://stackoverflow.com/questions/36393421/textviews-do-not-stretch-to-match-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!