开发者

Setting Textview's Attributes Programmatically

开发者 https://www.devze.com 2023-01-27 14:47 出处:网络
I have created 5 TextView programmatically, now i want to set few Parameters/Attributes of their such as Gravity, Layout_Gravity, etc.

I have created 5 TextView programmatically, now i want to set few Parameters/Attributes of their such as Gravity, Layout_Gravity, etc.

I know we can set it in XML layout at Design-time:

android:gravity="center|center_horizontal|center_vertical"
android:layout_gravity="center|center_horizontal|center_vertical"

But, How can we set Gravity/Layout_Gravity kinds of Attributes 开发者_高级运维programmatically?


You can set the gravity of TextView programmatically using setGravity(int))

Probably you can set layout_gravity like this(I've not yet tested this) :

TextView can let its parent know about layout preferences using

setLayoutParams(ViewGroup.LayoutParams params)
LayoutParams params=new LayoutParams(this, attrSet);
tv.setLayoutParams(params);


It's all in the docs. Look at the documentation for View, for example, under "XML Attributes". You'll see all XML attributes, and the corresponding method you'll need to call in code to get the same effect.

0

精彩评论

暂无评论...
验证码 换一张
取 消