Android的微调 - 当选项有不同的长度如何定位下拉箭头接近文本可能吗? [英] Android Spinner - How to position dropdown arrow as close to text as possible when options have different length?

查看:585
本文介绍了Android的微调 - 当选项有不同的长度如何定位下拉箭头接近文本可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

微调器中的选项具有不同的长度,当前,下拉箭头基于最长的选项位于右侧,如下面的屏幕截图所示.

The options in my spinner has different length and currently the dropdown arrow is positioned far to the right based on the longest option, as shown in the screenshot below.

是否可以移动下拉箭头,使其根据当前选择的选项动态定位?

Is it possible to move the dropdown arrow so that it is dynamically positioned based on currently selected option?

特别是当第一个选项只是'All'时,当下拉箭头距离右侧很远时,它看起来很奇怪.

Especially when the first option is just 'All', it looks weird when the dropdown arrow is so far away to the right.

请参阅Google翻译应用,其中下拉箭头始终位于其文本旁边:

Referring to Google Translate App where dropdown arrow is always positioned next to its text:

推荐答案

您无法控制原始下拉菜单图标的位置,唯一的方法是禁用默认图标并将自己的图标添加到下拉菜单中.

You cannot control the position of the original dropdown icon, the only way is to disable the default icon and add your own one into the dropdown.

首先,通过将微调框的背景设置为@null来禁用默认的下拉图标:

First, disable the default dropdown icon by setting the background of the Spinner to @null:

<Spinner
     android:id="@+id/spinner_main"
     android:spinnerMode="dropdown"
     android:background="@null"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"/>

然后使用仅一个TextView创建布局资源 spinner_item_main.xml ,我们可以在其右侧设置可绘制对象(您可以从

Then create a layout resource spinner_item_main.xml with only one TextView which we can set a drawable on its right side (you can download the arrow picture from here):

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textStyle="bold"
    android:gravity="left"
    android:textColor="@color/colorWhite"
    android:drawableRight="@drawable/ic_arrow_drop_down_white_24dp"
    />

最后在初始化微调器时设置此布局资源,还可以提供资源作为下拉视图(如我所做的那样):

Finally Set this layout resource when you initialize the Spinner, You can also provide a resource as the dropdown view (as what I have done):

(我用Kotlin)

spinner_main.adapter = ArrayAdapter<String>(this,
            R.layout.spinner_item_main, objects).apply {
        setDropDownViewResource(R.layout.spinner_dropdown_view_main)
    }

做吧! 在我的APP"中查看

这篇关于Android的微调 - 当选项有不同的长度如何定位下拉箭头接近文本可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆