代码之家  ›  专栏  ›  技术社区  ›  Mena

Android spinner弹出式下拉列表位置和大小

  •  0
  • Mena  · 技术社区  · 7 年前

    我设法改变了微调器的背景和它的弹出式下拉背景。我可以调整弹出窗口 android:dropDownHorizontalOffset android:dropDownVerticalOffset . 我还有一个微调器项目的自定义布局。

    但是,我被这个表情困住了:

    enter image description here

    请注意,这些项是如何超出弹出式下拉列表的边界的。我该怎么修?有没有办法在物品的顶部和底部插入填充物?

    <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"
             />
    

    微调器\项目\自定义\布局:

    <?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>
    

    2 回复  |  直到 7 年前
        1
  •  1
  •   Community Mohan Dere    6 年前

    因为您使用png文件作为微调器的弹出背景,所以有2个限制

    解决方案: 使用9补丁图像。你可以从Android官方网站上看一下如何使用它 draw9patch

    我刚用过 draw9patch 工具来编辑您的 rounded_rectangle_4.png

    rounded_rectangle_4.9.png

    你可以替换为你现在的背景,然后试试看。

        2
  •  1
  •   Vikash Bijarniya    7 年前

    您需要为微调器创建自定义适配器。 然后,在适配器类的getDropDownView方法中,需要使用下面的代码设置位置为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();
    }
    

    }