【问题标题】:How to customize drop down menu for spinner?如何自定义微调器的下拉菜单?
【发布时间】:2023-03-23 09:33:02
【问题描述】:

我需要在我的自定义微调器中自定义黄色下拉菜单,它需要移动到右侧,并且文本需要在其行的左侧对齐。

示例如下:

【问题讨论】:

    标签: android


    【解决方案1】:

    创建微调器时,您可以为显示分配两个不同的模板。一个用于选择,一个用于下拉。

    它在微调器的数据适配器中设置。这是我们使用的实际代码:

        _spinDestination = (Spinner) findViewById(R.id.spinDestination);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.custom_spinner
                , OrderDestRerouteDao.getList(_OrderDAO.getID(), _OrderDAO.getDestinationId()));
        adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        _spinDestination.setAdapter(adapter);
    

    dropdownviewresource 就是这里要注意的。它控制下拉菜单的外观。

    现在在您的页面布局中,将这些标签添加到您的微调器:

    android:dropDownHorizontalOffset = "?" 
    android:dropDownVerticalOffset = "?" 
    

    在哪里? = 一个偏移量(例如:50dp)

    布局更改将抵消您的下拉菜单。要获得它的任何其他样式(例如宽度),您需要为其自定义模板。

    根据要求,这是我的微调器的完整 xml。

                    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/corner_shape"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/tvCaption"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginBottom="-5dp"
            android:background="@drawable/upper_corner"
            android:gravity="center"
            android:text="@string/heading_order_reroute_page_title"
            android:textColor="@android:color/white"
            android:textSize="@dimen/heading_text_size"
            android:textStyle="bold" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rectangle"
            android:gravity="center_vertical"
            android:orientation="horizontal" >
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_weight="1"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:text="@string/label_current_destination"
                    android:textSize="@dimen/caption_text_size"/>
    
                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginStart="5dp"
                    android:layout_weight="0.1"
                    android:contentDescription="@drawable/star"
                    android:src="@drawable/star"
                    android:visibility="invisible"/>
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="2"
                android:orientation="vertical"
                android:layout_height="wrap_content">
    
                <TextView
                    android:id="@+id/tvCurrentDestination"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_margin="5dp"
                    android:layout_weight="3"
                    android:focusable="false"
                    android:gravity="start|center_vertical"
                    android:paddingStart="10dp"
                    android:textSize="@dimen/caption_text_size"/>
    
            </LinearLayout>
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rectangle"
            android:gravity="center_vertical"
            android:orientation="horizontal" >
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_weight="1"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.9"
                    android:gravity="center_vertical"
                    android:text="@string/label_new_destination"
                    android:textSize="@dimen/caption_text_size" />
    
                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginStart="5dp"
                    android:layout_weight="0.1"
                    android:contentDescription="@drawable/star"
                    android:src="@drawable/star" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="2"
                android:orientation="vertical"
                android:layout_height="wrap_content">
    
                <Spinner
                    android:id="@+id/spinDestination"
                    style="@android:style/Widget.Spinner"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_margin="5dp"
                    android:dropDownHorizontalOffset = "?"
                    android:dropDownVerticalOffset = "?" 
                    android:background="@drawable/bg_edit_text"
                    android:gravity="start|center_vertical"
                    android:paddingStart="10dp"
                    android:spinnerMode="dropdown" />
    
            </LinearLayout>
    
        </LinearLayout>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/divider_color" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rectangle"
            android:gravity="center_vertical"
            android:orientation="horizontal" >
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_weight="1"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.9"
                    android:gravity="center_vertical"
                    android:text="@string/label_reroute_notes"
                    android:textSize="@dimen/caption_text_size" />
    
                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginStart="5dp"
                    android:layout_weight="0.1"
                    android:contentDescription="@drawable/star"
                    android:src="@drawable/star" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="2"
                android:orientation="vertical"
                android:layout_height="wrap_content">
    
                <EditText
                    android:id="@+id/etRerouteNotes"
                    android:layout_width="match_parent"
                    android:layout_height="120dp"
                    android:layout_margin="5dp"
                    android:background="@drawable/bg_edit_text"
                    android:gravity="top|start"
                    android:paddingStart="10dp"
                    android:inputType="textMultiLine"
                    android:scrollbars="vertical"
                    android:fadeScrollbars="false"
                    android:textSize="@dimen/edit_text_size" />
    
            </LinearLayout>
    
        </LinearLayout>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/divider_color" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/btnReroute"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:background="@drawable/btn_forward"
                android:gravity="center"
                android:paddingBottom="10dp"
                android:paddingTop="10dp"
                android:text="@string/btn_complete_order_reroute_caption"
                android:textColor="@android:color/white"
                android:textSize="@dimen/button_text_size"
                android:textStyle="bold" />
    
            <Button
                android:id="@+id/btnCancel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:background="@drawable/btn_reject"
                android:gravity="center"
                android:paddingBottom="10dp"
                android:paddingTop="10dp"
                android:text="@string/btn_cancel_caption"
                android:textColor="@android:color/white"
                android:textSize="@dimen/button_text_size"
                android:textStyle="bold" />
    
        </LinearLayout>
    
    </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

    • 谢谢。为什么将其设置为 THIS 而不是适配器?
    • 您能举例说明如何在自定义微调器中实现它吗?因为在自定义中它看起来不起作用
    • 这是示例所在的类。实际上给我一点时间来粘贴不同的代码。我认为我们项目中的代码是错误的。
    • 好的,现在试试那个代码。我知道这个有效。我写了它所在的页面,它使用了两个不同的模板。主要的有一个假箭头,使它看起来像一个组合框。
    • 谢谢。但是我自己找到了解决方案,而且非常不同。我稍后会发布它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    相关资源
    最近更新 更多