Android 将微调器缩短到内容宽度
Android shorten spinner to content width
我正在使用微调器,用户可以选择他们的年龄吗,看起来像这样:
下拉箭头目前太靠右了。如何设置它,使微调器的宽度仅与微调器项目的内容所需的宽度一样短?
布局微调器部分的代码:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Age:" />
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/input_age_spinner"
android:layout_weight="0.6" />
</LinearLayout>
我曾尝试将微调器布局宽度设置为 wrap_content,但收效甚微。我已经尝试将微调器 layout_weight 设置为 0.2,这确实会使它变窄,但在那种情况下它不再左对齐
这样试试:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Age:" />
<Spinner
android:id="@+id/input_age_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
(或)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age:" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<Spinner
android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Age:" />
<Space
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/input_age_spinner"
android:layout_weight="0" />
</LinearLayout>
试试这个..
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Age:" />
<Spinner
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:id="@+id/input_age_spinner" />
</LinearLayout>
我正在使用微调器,用户可以选择他们的年龄吗,看起来像这样:
下拉箭头目前太靠右了。如何设置它,使微调器的宽度仅与微调器项目的内容所需的宽度一样短?
布局微调器部分的代码:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Age:" />
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/input_age_spinner"
android:layout_weight="0.6" />
</LinearLayout>
我曾尝试将微调器布局宽度设置为 wrap_content,但收效甚微。我已经尝试将微调器 layout_weight 设置为 0.2,这确实会使它变窄,但在那种情况下它不再左对齐
这样试试:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Age:" />
<Spinner
android:id="@+id/input_age_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
(或)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age:" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<Spinner
android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Age:" />
<Space
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/input_age_spinner"
android:layout_weight="0" />
</LinearLayout>
试试这个..
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Age:" />
<Spinner
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:id="@+id/input_age_spinner" />
</LinearLayout>