问题
Can anyone tell me what do I do wrong? :( I have 2 Spinners and two TextViews that I want to align right. I have set everything to right but still everything is left aligned.
Here is how it looks like:

But I want it too look like this:

And here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="right"
android:gravity="right">
<RadioGroup
android:id="@+id/radios"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="center_horizontal"
android:inputType="text"
android:onClick="test"
android:orientation="horizontal"
android:paddingRight="10dp" >
<RadioButton
android:id="@+id/rbtn_sell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:button="@null"
android:drawablePadding="3dp"
android:drawableRight="@android:drawable/btn_radio"
android:text="@string/label_sell_unicode"
android:textSize="12sp" />
<RadioButton
android:id="@+id/rbtn_rent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:drawablePadding="3dp"
android:drawableRight="@android:drawable/btn_radio"
android:text="@string/label_rent_unicode"
android:textSize="12sp"
android:paddingLeft="40dp"/>
</RadioGroup>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="right"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textView_from"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_from"
/>
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:gravity="right"
android:layout_gravity="right"
android:spinnerMode="dropdown" />
<TextView
android:id="@+id/textView_to"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_to"
/>
<Spinner
android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:gravity="right"
android:layout_gravity="right"
android:spinnerMode="dropdown" />
</LinearLayout>
Update: My XML after making the changes ElDuderino suggested. After this change the two TextView and the two Spinners are gone. They are nowhere on the screen.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<RadioGroup
android:id="@+id/radios"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="center_horizontal"
android:inputType="text"
android:onClick="test"
android:orientation="horizontal"
android:paddingRight="10dp" >
<RadioButton
android:id="@+id/rbtn_sell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:button="@null"
android:drawablePadding="3dp"
android:drawableRight="@android:drawable/btn_radio"
android:text="@string/label_sell_unicode"
android:textSize="12sp" />
<RadioButton
android:id="@+id/rbtn_rent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:drawablePadding="3dp"
android:drawableRight="@android:drawable/btn_radio"
android:text="@string/label_rent_unicode"
android:textSize="12sp"
android:paddingLeft="40dp"/>
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView_from"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="From" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView_to"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
回答1:
You made a mistake while adding the drawable on the radiobutton..Try this and lemme know
android:drawableLeft="@android:drawable/btn_radio"
回答2:
Use
android:layout_toRightOf="@id/textView_from"
android:layout_toRightOf="@id/textView_to"
this may help you
回答3:
The reason is this :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="right"
**android:gravity="right">**
You set the top layout to align everything to the right side, so your inner layout, which is set to wrap_content
doesn't take all the space available and so gets aligned right. So either remove the android:gravity
from the outer layout, OR set your inner layouts layout_width
to match_parent
.
Update, as it didn't work : Replace your inner linear layout with this : (I had to change string resources, but you'll figure out)...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView_from"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="From" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView_to"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
</LinearLayout>
</LinearLayout>
来源:https://stackoverflow.com/questions/23170717/how-can-one-align-spinners-right-to-left