[英]Is there a way to change the nested layout parameters of android widget programatically?
我想知道是否可以像正常活动中的LayoutParams
一样以编程方式更改android小部件xml
中嵌套Layout
的width
和height
?
我打算使用LineraLayouts
创建一个自定义进度栏,因此我需要更改一种布局的width
以显示更改
必须使用的LayoutParams
的类型由封装要更改其参数的内容的Layout的类型指定。
因此,例如,如果您想为LinearLayout
更改LayoutParams
,则需要检查封装它的Layout类型是什么。 在您的情况下, LinearLayout
由RelativeLayout
封装,因此您需要使用RelativeLayout.LayoutParams
而不是ViewGroup.LayoutParams
。
xml文件(activity_main.xml)中RelativeLayout
内的LinearLayout
。 使用RelativeLayout.LayoutParams
可以解决此问题。
更改此:
ll.setLayoutParams(new ViewGroup.LayoutParams(100,200));
至:
ll.setLayoutParams(new RelativeLayout.LayoutParams(100,200));
您必须使用这种方式来查找嵌套布局并更改布局参数。
Button button = (Button) findViewById(view.getId());
LinearLayout rLGreen = ((LinearLayout) button.getParent());
rLGreen.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
可能对您有帮助!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.