Reputation: 616
I've got some LinearLayout
objects with several things in them, including two TextView
s. Unfortunately, I've got a huge space between them and I don't know why.
See by yourself (between the content frame and the #642954 - 14/25/9814
thing):
https://i.sstatic.net/832QN.png
Here is my code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:contentDescription="@string/smiley"
android:padding="10dp"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:includeFontPadding="false"
>
<TextView android:id="@+id/contenu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/round"
android:padding="10dp"
android:layout_margin="5dp"
android:includeFontPadding="false"
android:layout_marginBottom="-10dp"
/>
<TextView android:id="@+id/infos"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:layout_margin="5dp"
android:includeFontPadding="false"
android:padding="0dp"
android:layout_marginTop="-10dp"
/>
</LinearLayout>
</LinearLayout>
Here is round.xml
:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#20000000" />
<corners android:radius="13dp" />
</shape>
</item>
<item android:bottom="2px">
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" />
<corners android:radius="13dp" />
<stroke
android:width="1dp"
android:color="#BBBBBB" />
</shape>
</item>
</layer-list>
Could you help me?
Thanks!
Upvotes: 1
Views: 7330
Reputation: 16064
No, no, no. You use negative margins, which may have unspecified behavior (presumably depending on Android OS version). I've tested this on API 8, and negative margins simply don't work. Instead, set each and every margin to a custom non-negative value, like so:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:contentDescription="@string/smiley"
android:padding="10dp"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:includeFontPadding="false"
>
<TextView
android:id="@+id/contenu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/round"
android:includeFontPadding="false"
android:padding="10dp"
android:text="" />
<TextView
android:id="@+id/infos"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="0dp"
android:gravity="right"
android:includeFontPadding="false"
android:padding="0dp"
android:text="" />
</LinearLayout>
</LinearLayout>
Here, I've set all margins to 5dp
except for:
contenu
's bottom margin: 0dp
infos
' top margin: 0dp
And voila, all the space is gone. Set them to your liking to adjust the gap between first and second TextView
.
Edit
I've checked SDK sources for unspecified behavior I was writing above.
It turns out that when specifying margins for LinearLayout
children, you have two options one of which must you choose:
margin
property. This effectively sets all side margins to the value of margin
. Beware: This option has precedence, meaning, if you set non-negative value for margin
it will ignore any values which you have individually set for any side margins.You were setting global margin
value which effectively caused the marginTop
/marginBottom
values to be ignored.
Upvotes: 2
Reputation: 1631
try this for equal spacing
<TextView android:id="@+id/contenu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/round"
android:padding="10dp"
android:layout_margin="5dp"
android:includeFontPadding="false"
android:layout_marginTop="5dp"
/>
<TextView android:id="@+id/infos"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:layout_margin="5dp"
android:includeFontPadding="false"
android:padding="0dp"
android:layout_marginTop="5dp"
/>
Upvotes: 1