以编程方式重新设置 TextView 高度 [英] re-setting a TextView height programmatically
本文介绍了以编程方式重新设置 TextView 高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在将 textView 添加到 xml 文件中的主窗口后,我想重置它的高度.
I want to reset a textView height after I have added it to the main window in the xml file.
在相对布局内,
<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);
但没有任何改变.
推荐答案
你应该通过LayoutParams
来改变它:
LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
textView.setLayoutParams(params);
编辑
您不应该在代码中使用像素大小,为此使用尺寸:
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>
在代码中:
params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);
这篇关于以编程方式重新设置 TextView 高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文