如何在微调器选择器位图和下拉列表项之间添加 space
How to add space between bitmap of spinner selector and items of drop down list
我有以下微调器:
<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>
下拉列表显示如下:
当我select下拉列表中的一个项目时,如果它太长就会与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>
编辑 2020 年 2 月 2 日
很抱歉周末午餐。
如果您正在使用 androidx
依赖项(我建议您这样做),这里是我用来在微调器中正确添加文本和所选项目之间的间距的代码。
在你的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>
我有以下微调器:
<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>
下拉列表显示如下:
当我select下拉列表中的一个项目时,如果它太长就会与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>
编辑 2020 年 2 月 2 日
很抱歉周末午餐。
如果您正在使用 androidx
依赖项(我建议您这样做),这里是我用来在微调器中正确添加文本和所选项目之间的间距的代码。
在你的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>