【问题标题】:Android - Spinner not correctly aligning to end of parentAndroid - 微调器未正确对齐到父级的末尾
【发布时间】:2017-05-07 22:05:42
【问题描述】:

对于我的应用,我需要将微调器对齐到其父级的右端。

这是我的微调器 xml:

<Spinner
            android:id="@+id/createsub_cycle_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/imageView3"
            android:layout_alignParentEnd="true"
            android:layout_below="@+id/imageView2"
            android:layout_marginEnd="10dp"
            android:textColorHint="@android:color/white"
            android:textSize="18sp" />

看,alignParentEnd 设置为 true

当我运行应用程序时,结果如下:

“每周”右侧的空白不应该存在。它应该与蓝色布局的最末端对齐。

我该如何解决?

编辑:

这是整个布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<android.support.v7.widget.CardView
    android:id="@+id/createsub_card"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp">


    <ImageView
        android:id="@+id/createsub_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:scaleType="fitCenter"
        android:layout_gravity="center_horizontal"
        android:layout_margin="20dp" />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="80dp">

        <TextView
            android:id="@+id/createsub_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:textColor="@android:color/white"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/createsub_user_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="10dp"
            android:maxLines="1"
            android:onClick="onTitleClicked"
            android:textAlignment="center"
            android:textColor="@android:color/white"
            android:textSize="18sp" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_below="@id/createsub_title"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"
            android:background="@android:color/white" />

        <TextView
            android:id="@+id/createsub_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/createsub_title"
            android:layout_margin="10dp"
            android:textColor="@android:color/white"
            android:textSize="18sp" />

        <Spinner
            android:id="@+id/createsub_currency_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/imageView2"
            android:dropDownWidth="40dp"
            android:layout_alignParentEnd="true"
            android:layout_below="@id/createsub_user_title"
            android:layout_marginEnd="10dp" />

        <EditText
            android:id="@+id/createsub_user_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/imageView2"
            android:layout_alignTop="@id/createsub_currency_spinner"
            android:layout_toStartOf="@id/createsub_currency_spinner"
            android:inputType="numberDecimal"
            android:maxLines="1"
            android:textColor="@android:color/white"
            android:textSize="18sp"
            android:hint="0.00"
            android:textColorHint="@android:color/white"/>


        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_below="@id/createsub_price"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"
            android:background="@android:color/white" />

        <TextView
            android:id="@+id/createsub_cycle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/createsub_price"
            android:layout_margin="10dp"
            android:text="@string/cycle"
            android:textColor="@android:color/white"
            android:textSize="18sp" />

        <Spinner
            android:id="@+id/createsub_cycle_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/imageView3"
            android:layout_alignParentEnd="true"
            android:layout_below="@+id/imageView2"
            android:layout_marginEnd="10dp"
            android:textColorHint="@android:color/white"
            android:textSize="18sp" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_below="@id/createsub_cycle"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"
            android:background="@android:color/white" />

        <TextView
            android:id="@+id/createsub_firstbill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/createsub_cycle"
            android:layout_margin="10dp"
            android:text="@string/first_bill"
            android:textColor="@android:color/white"
            android:textSize="18sp" />


        <!--TODO: when clicked has to show datepickerdialog-->
        <EditText
            android:id="@+id/createsub_user_firstbill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/imageView4"
            android:layout_alignEnd="@+id/imageView4"
            android:layout_below="@+id/imageView3"
            android:inputType="date"
            android:maxLines="1"
            android:textColor="@android:color/white"
            android:textSize="18sp" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_below="@id/createsub_firstbill"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"
            android:background="@android:color/white" />

        <TextView
            android:id="@+id/createsub_duration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/createsub_firstbill"
            android:layout_margin="10dp"
            android:text="@string/duration"
            android:textColor="@android:color/white"
            android:textSize="18sp" />

        <Spinner
            android:id="@+id/createsub_duration_selector"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/imageView5"
            android:layout_alignParentEnd="true"
            android:layout_below="@+id/imageView4"
            android:layout_marginEnd="10dp"
            android:textColor="@android:color/white"
            android:textSize="18sp" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_below="@id/createsub_duration"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"
            android:background="@android:color/white" />

        <TextView
            android:id="@+id/createsub_remindme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/createsub_duration"
            android:layout_margin="10dp"
            android:text="@string/remind_me"
            android:textColor="@android:color/white"
            android:textSize="18sp" />

        <Spinner
            android:id="@+id/createsub_reminder_selector"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/createsub_user_firstbill"
            android:layout_below="@+id/imageView5"
            android:textColorHint="@android:color/white"
            android:textSize="18sp" />


    </RelativeLayout>
</android.support.v7.widget.CardView>

【问题讨论】:

    标签: android android-spinner


    【解决方案1】:

    您可以尝试以下两件事:

    1. 我注意到您将android:dropDownWidth 硬编码为40dp。 “每周”文本的长度不一定跨越40dp。尝试删除该属性。

    2. 尝试将微调器的背景设置为 null 以删除插入符号的空间

      <Spinner
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@null"/>
      

    【讨论】:

    • 感谢您的帮助,部分原因。另一件事是微调器基于一个数组,并且在该数组中有比“每周”更长的字符串,所以他为它们保留了空间。我通过在我的spinner_item.xml 中添加android:textAlignment="viewEnd" 来解决它,将文本与末尾对齐,从而消除间隙。
    【解决方案2】:

    删除这个

    android:layout_marginEnd="10dp"

    在cardview标签中去掉这个

    android:layout_margin="10dp";

    【讨论】:

    • 感谢您的回答。删除它后,它会向右移动 10dp,但不会移动到布局的最末端
    • 发布整个布局,
    • 请查看更新后的问题。我已经添加了整个布局。挺长的
    • 更新了答案
    • 删除 CardView 边距会删除我需要的外边距,但不能解决问题
    猜你喜欢
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 2022-01-08
    • 2016-04-11
    • 1970-01-01
    • 2014-09-28
    相关资源
    最近更新 更多