简体   繁体   English

android中如何动态设置线性布局的高宽

[英]How to set the height and width of linear layout dynamically in android

I have a Linearlayout in my xml which has fill_parent for both height and width... Now i want to dynamically change the height and width... I tried Using我的 xml 中有一个 Linearlayout,它的高度和宽度都有 fill_parent ......现在我想动态改变高度和宽度......我试过使用

_llChooseType.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,getWindowManager().getDefaultDisplay().getHeight()-100));

But it is giving me java.lang.ClassCastException: android.view.ViewGroup$LayoutParams exception...但它给了我 java.lang.ClassCastException: android.view.ViewGroup$LayoutParams 异常......

How to do it?怎么做?

Use this..用这个..

new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,getWindowManager().getDefaultDisplay().getHeight()-100));

If the _llChooseType is a LinearLayout object then I think you should use如果 _llChooseType 是 LinearLayout object 那么我认为你应该使用

new LinearLayout.LayoutParams(...);

Regards问候

Ziteng Chen陈子腾

The setLayoutParams widht & height can only have the following 3 values: FILL_PARENT, MATCH_PARENT and WRAP_CONTENT. setLayoutParams widht & height 只能有以下 3 个值:FILL_PARENT、MATCH_PARENT 和 WRAP_CONTENT。

You should use public void layout (int l, int t, int r, int b) To actually change the layout of the created view/viewgroup.您应该使用public void layout (int l, int t, int r, int b)来实际更改创建的视图/视图组的布局。

Do this... Type:这样做... 键入:

_llChooseType.setLayoutParams(new Layo

and press ctrl+space_bar.然后按 ctrl+space_bar。 This will give you a list of LayoutParams options to choose from...select LinearLayout.LayoutParams and write the rest of the code as is.这将为您提供 LayoutParams 选项列表以供您选择...select LinearLayout.LayoutParams 并按原样编写代码的 rest。 The problem here is that the LayoutParams you use are ViewGroup.LayoutParams and they don't apply to linear layouts!这里的问题是您使用的 LayoutParams 是 ViewGroup.LayoutParams 并且它们不适用于线性布局!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在android中动态增加layout(linear / relative)布局高度? - how to increase layout(linear/relative) layout height dynamically in android? 如何在Android线性布局中使用50%的宽度和高度 - how to use 50% width and height in android linear layout 如何在android中以编程方式获取相对-线性布局的宽度和高度? - How to get programmatically width and height of Relative - Linear layout in android? 无法设置水平线性布局的宽度或垂直线性布局的高度 - Not able to set width of a horizontal linear layout or height of vertical linear layout 如何在 Android 中设置布局的宽度和高度? - How to set the width and height of a layout in Android? 如何动态设置FrameLayout的宽度和高度android? - How to set Width and Height of FrameLayout dynamically android? 如何在android中动态设置相对布局的高度 - how to set height of relative layout dynamically in android Android:如何将垂直线性布局中的两个按钮设置为相同的宽度? - Android: how to set two buttons in vertical linear layout to same width? 如何在Android中将线性布局的所有元素设置为相同的宽度? - How to set all elements of linear layout to be same width in android? 如何在Android中使用解析器动态设置线性布局中的背景图像 - how to set background image in Linear layout dynamically using Parser in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM