I need to implement Dialog for my Android app through Java code, so I can't use XML.
I have root LinearLayout
where I implement range seek bar, then I have another LinearLayout
under root layout, with horizontal orientation, where I want to add two buttons in same row. So I need to set weight to 1, and width to FILL_PARENT
and height to WRAP_CONTENT
.
How I can do that with Java code?
LinearLayout. LayoutParams param = new LinearLayout. LayoutParams( 0, LayoutParams. MATCH_PARENT, 1);
LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.
This set of layout parameters defaults the width and the height of the children to ViewGroup. LayoutParams. WRAP_CONTENT when they are not specified in the XML file. RelativeLayout.LayoutParams. Specifies how a view is positioned within a RelativeLayout .
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); p.weight = 1; rangeSeekBar.setLayoutParams(p);
I'm not sure which view you want to set the layout params on. I just assumed the rangeSeekbar to show an example. Change if you need.
When using the layout params always use the root's param type..
Ex. if you have a View
you want to apply params to within a RelativeLayout
use RelativeLayout.LayoutParams
..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With