[英]How to programmatically set the width of the LinearLayout?
Is the source of the slider and the lesson link:是滑块的来源和课程链接:
http://www.oodlestechnologies.com/blogs/Facebook-Style-Slide-Menu-In-Android http://www.oodlestechnologies.com/blogs/Facebook-Style-Slide-Menu-In-Android
In this case, the width of the file "left_menu.xml" determined TextView (android:layout_width="260dp") How do I set the width to "LinearLayout" file "left_menu.xml" depending on the device screen?在这种情况下,文件“left_menu.xml”的宽度决定了 TextView (android:layout_width="260dp") 如何根据设备屏幕将宽度设置为“LinearLayout”文件“left_menu.xml”? For example, I want the width of the "LinearLayout" was always 1/3 of the screen device?
例如,我希望“LinearLayout”的宽度始终是屏幕设备的 1/3? Or any way to set the width of the TextView 1/3 of the width of the device screen.
或者任何方式将TextView的宽度设置为设备屏幕宽度的1/3。
To set the LinearLayout
or TextView
width to 1/3 of the device screen: 要将
LinearLayout
或TextView
宽度设置为设备屏幕的1/3 ,请执行以下操作:
first of all get the device screen width: 首先获取设备的屏幕宽度:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
try {
display.getRealSize(size);
} catch (NoSuchMethodError err) {
display.getSize(size);
}
int width = size.x;
int height = size.y;
now just create a LayoutParams
and set it to the Text: 现在只需创建一个
LayoutParams
并将其设置为Text:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)(width/3),
LinearLayout.LayoutParams.WRAP_CONTENT); // or set height to any fixed value you want
your_layout.setLayoutParams(lp);
// OR
your_textView.setLayoutParams(lp);
LinearLayout layout = (LinearLayout)findViewById(R.id.ll);
LayoutParams params = (LayoutParams) layout.getLayoutParams();
params.height = 100;
params.width = 100;
LinearLayout linear = (LinearLayout) findViewById(R.id.ll);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linear.setLayoutParams(params);
As you can see, you can set Integer values for the LinearLayout.LayoutParams() constructor, like this: 如您所见,您可以为LinearLayout.LayoutParams()构造函数设置Integer值,如下所示:
LinearLayout.LayoutParams cellParams = new LinearLayout.LayoutParams(0, 100);
The costructor wants pixels and not dp(Density Pixels), here's a formula to convert PXs from DPs: 构造函数需要像素而不是dp(密度像素),这是一个将DP转换为DP的公式:
(int) (<numberOfDPs> * getContext().getResources().getDisplayMetrics().density + 0.5f)
The above answers are correct. 以上答案是正确的。 I'll just suggest future readers to take a look at their xml layout file and be sure that the LinearLayout element is not using weight attribute.
我只是建议未来的读者看一下他们的xml布局文件,并确保LinearLayout元素未使用weight属性。 If so, consider updating weight in your LayoutParams object.
如果是这样,请考虑更新LayoutParams对象中的权重。 Another good practice is to retrieve the layout params object from the view you're trying to adjust, instead of creating one from scratch and having to set all the values.
另一个好的做法是从您要调整的视图中检索layout params对象,而不是从头开始创建一个布局对象,而不必设置所有值。
LinearLayout YOUR_LinearLayout =(LinearLayout)findViewById(R.id.YOUR_LinearLayout)
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ 100,
/*weight*/ 1.0f
);
YOUR_LinearLayout.setLayoutParams(param);
The easy way to do that, set value of layoutParams, and please notice this size is not in DP.最简单的方法是设置 layoutParams 的值,请注意这个大小不在 DP 中。
view.layoutParams.width = 400;
view.layoutParams.height = 150;
view.requestLayout();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.