重新编程设置一个TextView高度高度、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...

我想重新设置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);
阅读全文

相关推荐

最新文章