Setting Textview's Attributes Programmatically
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.
精彩评论