我有以下 Spinner :
<Spinner
android:id="@+id/safe_network_time_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/spinner_selector"
android:entries="@array/schedule_edit_days_repeat"
android:popupBackground="@drawable/custom_dropdown_white_background"
android:spinnerMode="dropdown"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/divider4"
tool:listitem="@layout/custom_dropdown_spinner_button" />
spinner_selector为:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:gravity="right|center_vertical" android:src="@drawable/down_red_arrow" />
</item>
</selector>
下拉列表如下所示:
当我选择下拉列表中的一个项目时,如果它太长,它将与arrov位图重叠。我该如何避免呢?
尝试使用 layer-list
bg_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<!-- This is the actual spinner background -->
<selector >
<!-- Feel free to use your own drawable for these states -->
<item android:state_window_focused="false" android:drawable="@drawable/bg_border_accent_thin"/>
<item android:state_pressed="true" android:drawable="@drawable/bg_border_accent_thin"/>
<item android:state_window_focused="true" android:state_pressed="false" android:drawable="@drawable/bg_border_grey_thin"/>
</selector>
</item>
<!-- This is the spinner drawable, use your custom drawable aswell. android:right does the magin to separate this drawable from the spinner content-->
<item android:drawable="@drawable/ic_ripple_arrow"
android:gravity="center_vertical|right"
android:right="16dp"/>
</layer-list>
我为周末的午餐而道歉。
如果您正在使用androidx
依赖项(建议您这样做),那么这是我用来在文本和 Spinner 中所选项目之间正确添加间距的代码。
在您的layout.xml上
<!-- The line I think you need is the one giving padding to the right -->
<androidx.appcompat.widget.AppCompatSpinner
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:paddingRight="36dp"
android:background="@drawable/bg_spinner"
android:popupBackground="@drawable/bg_spinner_popup"/>
的bg_spinner.xml
是提供一对夫妇上述线中的一条。
弹出窗口实际上并不重要,因为它给出了一些有关如何绘制背景的指令,没有什么重要的,但是无论哪种方式……
bg_spinner_popup.xml
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android
android:shape="rectangle">
<corners android:radius="3dp"/>
<solid android:color ="@android:color/white"/>
<stroke android:width="5px" android:color="@color/primaryColor"/>
</shape>
给定的代码必须在您的
resources/drawable
文件夹中。右键单击那里,add new drawable resource
是的,我在一个新的drawable中添加了您的代码,并将其用作微调器的背景,但似乎出了点问题,箭头被拉伸了,就像我发布的新图片一样
午饭后让我给你一个答案:)
有用。谢谢
尝试使项目视图为match_parent并将文本视图标签设置为重力右