以编程方式调整线性布局的高度

时间:2016-07-12 00:08:32

标签: java android android-layout android-linearlayout

每当我尝试设置LinearLayout的高度时,我总是会遇到这个异常:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams

这是我的代码:

LinearLayout.LayoutParams  hide = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
LinearLayout.LayoutParams  show = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100);

driverMainOptions.setLayoutParams(hide);
mapDirections.setLayoutParams(show);

我需要一个特定的导入语句来正确执行吗?

1 个答案:

答案 0 :(得分:1)

这应该有效

LinearLayout lLayout = new LineaLayout(context);
LayoutParams params = lLayout.getLayoutParams();

params.height = 200;
params.with = 200;
lLayout.setLayoutParams(parmas);

Android: How to Programmatically set the size of a Layout

查看已接受的答案