how to change programmatically?
This is my view, and I wish to change layout_width to "10dip". How do I do so programmatically? Note, this is not a Linear Layout, it's a view.
<View android:id="@+id/nutrition_bar_filled"
android:background="@drawable/green_rectangle"
android:layout_height="30dip"
android:layout_width="50dip">
</View>
I know about LayoutParams. How do I use it to set the width to 10dip?
我相信你的问题是动态改变视图的宽度,而上面的方法会将布局属性完全更改为新的布局属性,所以我建议首先从视图中获取LayoutParams,然后在layoutParmas上设置宽度,最后将layoutparams设置为视图,以下下面的步骤做同样的事情。
View view_instance = (View)findViewById(R.id.nutrition_bar_filled);
LayoutParams params=view_instance.getLayoutParams();
params.width=newOne;
view_instance.setLayoutParams(params);
你也可以像这样设置高度和宽度:
viewinstance.setLayoutParams(new LayoutParams(width, height));
try using
View view_instance = (View)findViewById(R.id.nutrition_bar_filled);
view_instance.setWidth(10);
use Layoutparams to do so where you can set width and height like below.
LayoutParams lp = new LayoutParams(10,LayoutParams.wrap_content);
View_instance.setLayoutParams(lp);
链接地址: http://www.djcxy.com/p/93290.html
上一篇: 在Android中,如何以编程方式在dp中设置页边距?
下一篇: 如何以编程方式更改?