I'm trying to have a layout in a certain width, using the layout_width
property, but when inflated - the width of the layout is taken from the child, which is smaller then the layout_width
I stated. Here's an example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/blabla"
android:orientation="vertical"
android:layout_width="310dip"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="60dip"
android:paddingTop="15dip"
android:paddingLeft="23dip"
android:text="udini "
/>
</LinearLayout>
This code make the entire layout be in the width of 60dip
.
But when adding the minWidth
attribute to the layout, with the value 310dip
, then the width of the layout is as expected.
Why is it like that? Is the layout_width
attribute just a suggestion?
Thanks,
Udi
So Linear Layout should preferred over Relative Layout! Also if you use weight_sum attribute within Linear Layout it would again “measure the child twice”. So avoid using this attribute as much as possible.
LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.
Definitions: Frame Layout: This is designed to block out an area on the screen to display a single item. Linear Layout: A layout that arranges its children in a single column or a single row. Relative Layout: This layout is a view group that displays child views in relative positions.
LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute.
LinearLayout
s are not required to honour the layout_width
attribute when it's not set to wrap_content
or fill_parent
.
You can still use android:minWidth
if that's enough for you, or other options are using the android:layout_weight
attribute or increase the android:padding
attribute on the child.
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