Android 微调器弹出下拉位置和大小
Android spinner popup dropdown position and size
我设法更改了 Spinner 背景及其弹出下拉菜单背景。我可以调整弹出 android:dropDownHorizontalOffset
和 android:dropDownVerticalOffset
。
我也有微调项的自定义布局。
但是,我坚持这个样子:
注意项目是如何超出弹出下拉列表的范围的。我该如何解决?有没有办法以某种方式为项目的顶部和底部插入填充?
<Spinner
android:id="@+id/branchesSpinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:background="@drawable/dropdown_base"
android:dropDownHorizontalOffset="-5dp"
android:dropDownVerticalOffset="50dp"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:popupBackground="@drawable/rounded_rectangle_4"
/>
spinner_item_custom_layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="@color/charcoal"
android:textStyle="bold"
tools:text="Select Branch"></TextView>
N.B: 我添加的背景是.PNG图片而不是XML
因为您使用 png 文件作为微调器的弹出背景,所以有 2 个限制
- 如果您 运行 在屏幕密度高的设备上使用该应用程序,您的微调器背景可能会拉伸并且看起来很难看。
- 您无法为弹出式背景设置填充,这就是为什么有时您的内容看起来不像您预期的那样。
解决方案: 使用 9 补丁图像。您可以从 Android 官方网站查看有关如何使用 draw9patch
的指南
我刚刚使用 draw9patch
工具编辑了您的 rounded_rectangle_4.png
。
您可以替换成您当前的背景然后试试看。
您需要为微调器创建自定义适配器。
然后,在适配器的 getDropDownView 方法中 class 您需要使用以下代码
在位置为 0 时设置下拉项的上边距
public static void setMargins (View v, int l, int t, int r, int b) {
if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
p.setMargins(l, t, r, b);
v.requestLayout();
}
}
只是让你的逻辑来比较下拉子项的位置。
请注意,您只需要设置上边距。
我设法更改了 Spinner 背景及其弹出下拉菜单背景。我可以调整弹出 android:dropDownHorizontalOffset
和 android:dropDownVerticalOffset
。
我也有微调项的自定义布局。
但是,我坚持这个样子:
注意项目是如何超出弹出下拉列表的范围的。我该如何解决?有没有办法以某种方式为项目的顶部和底部插入填充?
<Spinner
android:id="@+id/branchesSpinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:background="@drawable/dropdown_base"
android:dropDownHorizontalOffset="-5dp"
android:dropDownVerticalOffset="50dp"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:popupBackground="@drawable/rounded_rectangle_4"
/>
spinner_item_custom_layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="@color/charcoal"
android:textStyle="bold"
tools:text="Select Branch"></TextView>
N.B: 我添加的背景是.PNG图片而不是XML
因为您使用 png 文件作为微调器的弹出背景,所以有 2 个限制
- 如果您 运行 在屏幕密度高的设备上使用该应用程序,您的微调器背景可能会拉伸并且看起来很难看。
- 您无法为弹出式背景设置填充,这就是为什么有时您的内容看起来不像您预期的那样。
解决方案: 使用 9 补丁图像。您可以从 Android 官方网站查看有关如何使用 draw9patch
的指南我刚刚使用 draw9patch
工具编辑了您的 rounded_rectangle_4.png
。
您可以替换成您当前的背景然后试试看。
您需要为微调器创建自定义适配器。 然后,在适配器的 getDropDownView 方法中 class 您需要使用以下代码
在位置为 0 时设置下拉项的上边距public static void setMargins (View v, int l, int t, int r, int b) {
if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
p.setMargins(l, t, r, b);
v.requestLayout();
}
}
只是让你的逻辑来比较下拉子项的位置。 请注意,您只需要设置上边距。