How can I dynamically change element properties?
I'm looking for a way to dynamically change an elements properties.
For example I want the user of my app to be able to enter a value within my program that will change android:layout_marginTop="blabla开发者_运维技巧" to whatever I want. It doesn't specifically have to be that property, I want to be able to change any property of any element. Is there a way to do this?
Depends which kind of property you are willing to change.
For some you have methods in the View
object and for other using LayoutParameters
.
You can check the following questions which answers similar stuff:
- Set margins in a LinearLayout programmatically
- How to set margin of ImageView using code, not xml
You can use this way by defining layout params and define it in your view.
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp.setMargins(30, 0, 0, 0);
lp.height = "100";
lp.width = "100";
button.setLayoutParams(lp);
精彩评论