Java 如何在Android中设置布局的宽度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1357577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:06:52  来源:igfitidea点击:

How to set the width of a layout in Android?

javaandroid

提问by

Is there Any way to set the width of the Layout.

有没有办法设置布局的宽度。

Because most of the time it doesn't work?

因为大多数时候它不起作用?

回答by stevedbrown

<WhateverLayout ... android:layout_width='xxxdip' />

回答by Isaac Waller

Try adding the layout_widthor widthattributes to your layout. Example:

尝试将layout_widthwidth属性添加到您的布局。例子:

<LinearLayout android:layout_width="100dip" android:layout_height="100dip" />

回答by Jeremy Logan

If you're doing it the XML way you'd use the layout_width attribute:

如果您以 XML 方式使用 layout_width 属性:

<LinearLayout
    android:layout_width ="100dp"
    android:layout_height="wrap_content" />

If you're doing it programatically you'd use the LayoutParams:

如果您以编程方式执行此操作,您将使用 LayoutParams:

LinearLayout ll = new LinearLayout(this);
parentView.addView(ll, new LinearLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT));