以编程方式重新设置TextView高度

39
我想在将textView添加到xml文件的主窗口后重置它的高度。
在RelativeLayout内部。
  <TextView
      android:id="@+id/text_l"
      android:layout_width="50sp"
      android:layout_height="50sp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginLeft="10sp"
      android:layout_marginTop="145dp"
      android:gravity="center"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:textColor="#000000" >
  </TextView>

我只是想将它从50更改为70:

我尝试过:

 TextView text = (TextView)findViewById(R.id.text_l);
 text.setHeight(70);

但是没有任何变化。


尝试这个:https://dev59.com/93A75IYBdhLWcg3wAUB9 - Nitesh Khosla
6个回答

111

你应该通过 LayoutParams 进行更改:

LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
textView.setLayoutParams(params);

编辑

您的代码中不应该使用像素大小,而应该使用 dimensions 代替:

dimens.xml:

<dimen name="text_view_height">50dp</dimen>
在代码中:
params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);

这里奇怪的事情是:textview.setheight(...) 在我的一些测试设备上确实起作用,尽管与TextView的setHeight函数的文档相反,它并不会改变视图的布局参数。但在我的一个测试设备(Xperia ray,Android 4.0.4)中,这仅在横向模式下起作用,而在纵向模式下则不起作用。按照您建议的在布局参数中更改高度在我所有的测试设备上都可以正常工作。 - Nantoka
它对我没用。恐怕它在新的API中不起作用。一旦TextView被分层,设置TextView的高度就没有效果了。 - cesards
1
请注意,TextView textView = new TextView(context); textView.getLayoutParame() 将为 null。在使用之前应该判断是否为 null。 - BertKing

11

您可以通过实用的方式来设置TextView的高度:

private TextView mTxtView;
int height = 50; //your textview height
mTxtView.getLayoutParams().height = height;

4
我更喜欢这样做,而不是找到正确的Layoutparams类。 - Roisgoen
那是真的 @Roisgoen - Dhruv Raval
需要特别注意的是,如果您使用textView来获取LayoutParams(),最好判断是否为空。 - BertKing

4

您可以通过以下方法动态设置TextView的宽度和高度:

private TextView mTxtView;
private int screenWidth, screenHeight;
Display display = getWindowManager().getDefaultDisplay(); 
    screenWidth = display.getWidth();
    screenHeight = display.getHeight();


LayoutParams params = mTxtView.getLayoutParams();
    params.width = screenWidth-30;
    mTxtView.setLayoutParams(params);

2
在Kotlin中使用DP转像素的翻译
最初的回答
  changeTextHeight.setOnClickListener { view ->

         // random height for testing
        val randomHeightInDP = Random().nextFloat() * (50.0f - 10.0f) + 10

        // set Height in pixels
        hello.layoutParams.height = convertDpToPixel(randomHeightInDP, applicationContext)
         //refresh layout
        hello.requestLayout()


    }

将DP转换为像素,请参阅此帖子:

最初的回答:

fun convertDpToPixel(dp: Float, context: Context): Int {
        return (dp * (context.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)).toInt()
    }

2

如果你想使用dimen,以下是示例方法:

首先,在dimen XML文件中设置大小。

<dimen name="text_height">50dp</dimen>
<dimen name="text_width">50dp</dimen>

并且

text_l.getLayoutParams().height = 
     getResources().getDimensionPixelSize(R.dimen.text_height);

text_l.getLayoutParams().width = 
     getResources().getDimensionPixelSize(R.dimen.text_width);

或者

如果您想设置整数值(例如,我们想要设置50dp的高度和100dp的宽度)

text_l.getLayoutParams().height = 50 * 3;
text_l.getLayoutParams().width= 100 * 3;

1

我知道这是一个老问题,但为了那些可能会找到它的人,有些情况下你应该在更改布局参数后调用textView.requestLayout()。如果你只是在绘制布局之前一次性更改布局参数,则可以省略它。在我的情况下,我想根据单选按钮选择更改TextView的高度参数,使用onCheckedChangedListener,但TextView的高度只会在第一次绘制时更新。添加requestLayout()解决了这个问题。

TextView tv;
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
if(!tv.isInLayout()) {//make sure it isn't currently in a layout pass 
    tv.requestLayout();//request a layout pass
 }

你帮了我,虽然这太老了,但你永远不知道:D - Milos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接