重新编程设置一个TextView高度 [英] re-setting a TextView height programmatically

查看:224
本文介绍了重新编程设置一个TextView高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重新设置TextView的高度后,我已经把它添加到在XML文件中的主窗口。

I want to reset a textView height after I have added it to the main window in the xml file.

里面RelativeLayout的,

inside a 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:

I just want to change it from 50 to 70:

我想:

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

但没有任何改变。

but nothing changed.

推荐答案

您应该通过的LayoutParams 更改

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

修改

您不应使用尺寸以像素为单位在您code,使用尺寸是:

You should not use sizes in pixels in you code, use dimensions for this:

dimens.xml:

dimens.xml:

<dimen name="text_view_height">50dp</dimen>

在code:

params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);

这篇关于重新编程设置一个TextView高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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