【问题标题】:Android shorten spinner to content widthAndroid将微调器缩短到内容宽度
【发布时间】:2016-06-10 11:46:22
【问题描述】:

我正在使用微调器,用户可以选择他们的年龄,看起来像这样:

下拉箭头当前太靠右了。如何设置它以使微调器宽度仅与微调器项目内容所需的一样短?

布局的微调器部分的代码:

<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,这确实使其变窄,但在这种情况下它不再左对齐

【问题讨论】:

    标签: android android-layout android-widget android-xml


    【解决方案1】:

    试试这样:

     <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>
    

    【讨论】:

    • 有没有办法用线性布局做到这一点?
    • @Simon,编辑了我的答案,但最好使用第一个这样的相对布局。第二个是复杂的布局。
    • 在使用 LinearLayout 时在文本视图和微调器之间放置一个空格。给它的权重为 1,文本和微调器的权重为 0
    • 谢谢,这使微调器变小了,但android:gravity=left 似乎没有做任何事情,因为微调器与父容器的右侧对齐。即微调器应该像其他输入小部件一样向左对齐
    • android:gravity=left 用于微调器的内容。如果你不给这个,文字会出现在中心。
    【解决方案2】:
    <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>
    

    【讨论】:

      【解决方案3】:

      试试这个..

      <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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 2017-02-21
        • 2011-12-03
        • 2013-11-15
        相关资源
        最近更新 更多