问题
I'm having problems with a layout. I have this structure:
<ScrollView>
<LinarLayout>
<Button/>
<RelativeLayout>
<ImageButton/>
<ImageButton/>
<ImageButton/>
</RelativeLayout>
</LinarLayout>
</ScrollView>
I want the three ImageButtons centered and one next to the other like this:
IMG1 IMG2 IMG3
For this purpose, I've centered the elements inside the RelativeLayout
using android:gravity="center"
and I've used android:layout_toRightOf="@+id/element"
and android:layout_toLeftOf="@+id/element"
to order the elements the way I want.
When I check the result on the GraphicalLayout
option, it is showing exactly what I want (I've never had problems with this view, it always shows the elements as I write on the XML file) but then I compile and execute and it is all disordered. I've tried a lot of different ways to order the elements but it is doing what it want... This is the first time it happened to me in almost two years of development.
I've also closed Eclipse but nothing's changed.
EDIT to add information
I want the elements to be like this:
IMG1 IMG2 IMG3
In the GraphicalLayout it is all ok but in the emulator the three IMGS are overlapping.
Here's the complete code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bugbox_wall_port" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/writeNewMsg"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp" >
<ImageButton
android:id="@+id/spyMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/msgspy" />
<ImageButton
android:id="@+id/playerMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/msgplyr"
android:layout_toRightOf="@+id/spyMsgs" />
<ImageButton
android:id="@+id/allyMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/msgally"
android:layout_toRightOf="@+id/playerMsgs" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
I do not know how to solve it. Is driving me nuts. What's the problem with this layout? Why the elements aren't ordered? How can I solve it and order the elements as I do always?
Can someone help me?
Thank you in advance!
回答1:
small idea:
how about using android:src
instead of android:background
?
回答2:
Maybe this is what you are looking for:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/writeNewMsg"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:padding="10dp" >
<ImageButton
android:id="@+id/spyMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon"/>
<ImageButton
android:id="@+id/playerMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon"
android:layout_toRightOf="@id/spyMsgs" />
<ImageButton
android:id="@+id/allyMsgs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon"
android:layout_toRightOf="@id/playerMsgs" />
</RelativeLayout>
</LinearLayout>
</ScrollView>

来源:https://stackoverflow.com/questions/11749552/xml-is-crazy-it-is-not-showing-real-position-of-elements