如何将微调器从右到左对齐? [英] How can one align spinners right to left?
问题描述
谁能告诉我我做错了什么?:(我有 2 个 Spinners 和两个要对齐的 TextViews.我已将所有内容都设置为正确,但仍然所有内容都左对齐.
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.
这是它的样子:
但我也希望它看起来像这样:
But I want it too look like this:
这是我的 XML 代码:
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>
更新:在进行 ElDuderino 建议的更改后我的 XML.在此更改之后,两个 TextView 和两个 Spinner 都消失了.他们不在屏幕上.
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>
推荐答案
原因是这样的:
<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">**
您将顶部布局设置为将所有内容与右侧对齐,因此设置为 wrap_content
的内部布局不会占用所有可用空间,因此会正确对齐.因此,要么从外部布局中删除 android:gravity
,要么将内部布局 layout_width
设置为 match_parent
.
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>
这篇关于如何将微调器从右到左对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!