简体   繁体   中英

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

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? For example, I want the width of the "LinearLayout" was always 1/3 of the screen device? Or any way to set the width of the TextView 1/3 of the width of the device screen.

To set the LinearLayout or TextView width to 1/3 of the device screen:

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:

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 cellParams = new LinearLayout.LayoutParams(0, 100);   

The costructor wants pixels and not dp(Density Pixels), here's a formula to convert PXs from DPs:

(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. If so, consider updating weight in your LayoutParams object. 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.

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.

view.layoutParams.width = 400;
view.layoutParams.height = 150;
view.requestLayout();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM