TextView不会拉伸到MATCH_PARENT [英] TextViews do not Stretch to MATCH_PARENT

查看:118
本文介绍了TextView不会拉伸到MATCH_PARENT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Android应用程序,要求我以编程方式将TextView添加到TableRow中,然后又将其添加到TableLayout中.为了使设计看起来正确,TextView的宽度必须与TableRow的宽度相同.我遇到的问题是,即使我尝试使用 MATCH_PARENT 将TextView的宽度设置为匹配,也无法生效.

这是我要用来执行此操作的Java示例:

  final TableRow myTableRow = new TableRow(getApplicationContext());最终的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);//假设已经声明了"myTableLayout". 

这是 myTableLayout 的XML:

 < TableLayoutandroid:layout_width ="match_parent"android:layout_height ="match_parent"android:id ="@ + id/myTableLayout"><表格行android:layout_width ="match_parent"android:layout_height ="match_parent"android:id ="@ + id/tableRowOne">< TextViewandroid: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><表格行android:layout_width ="match_parent"android:layout_height ="match_parent"android:id ="@ + id/tableRowTwo">< TextViewandroid: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><表格行android:layout_width ="match_parent"android:layout_height ="match_parent"android:id ="@ + id/tableRowThree">< TextViewandroid: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> 

有人知道我在做什么错吗?

这是布局显示方式的图片.此处发布的代码与实际的应用程序代码不同,但足以显示问题.具体来说,删除的代码与答复有关.在作为问题一部分发布的示例代码中,Java代码中仅显示一个TextView,但是在实际项目中,有两个.

布局示例图像

解决方案

为视图赋予height和width属性的最佳方法是使用布局参数.大多数情况下(对我而言几乎所有时间)直接设置宽度和高度都不会起作用.

  TableRow.LayoutParams mParam = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);//设置布局参数以查看textView.setLayoutParams(mParam); 

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

解决方案

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);            

这篇关于TextView不会拉伸到MATCH_PARENT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆