样式微调器下拉列表

时间:2015-04-14 06:38:27

标签: android styles spinner

我有一些关于设置微调器

的问题
  1. 我需要更改微调器的drawable(选定视图) 当用户打开下拉列表时(我尝试了几个选择器 具有不同的状态,但没有正常工作)
  2. 我无法更改下拉列表中项目的文字颜色(我可以更改 颜色,但我需要不同颜色的选定值)你可以看到更多 以下链接中的详细信息。
  3. 我不能从下拉列表中删除分隔符。
  4. 我不想使用带按钮和列表的自定义布局来模拟微调器的功能(但如果没有解决方案,我会这样做)。

    style.xml

    <resources xmlns:android="http://schemas.android.com/apk/res/android">
    
    <style name="AppBaseTheme" parent="@android:style/Theme.Black.NoTitleBar.Fullscreen">
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:colorBackground">@android:color/white</item>
        <item name="android:dropDownSpinnerStyle">@style/SpinnerTheme</item>
    </style>
    
    <style name="SpinnerAppTheme" parent="android:Widget.Holo.Light.Spinner">
        <item name="android:background">@drawable/spinner_background_holo_light</item>
        <item name="android:dropDownSelector">@drawable/list_selector_holo_light</item>
        <item name="android:dropDownListViewStyle">@style/mySpinnerStyle</item>
    </style>
    
    <style name="mySpinnerStyle" parent="android:Widget.ListView.DropDown">
         <item name="android:divider">@null</item>
        <item name="android:dividerHeight">0px</item>
    </style>
    
    <style name="Theme_Dialog_Translucent" parent="android:Theme.Dialog">
        <item name="android:windowBackground">@null</item>
        <item name="android:background">@android:color/white</item>
        <item name="android:windowTitleStyle">@style/dialog_title_style</item>
    </style>
    
    <style name="Theme_Dialog_Measurment_Data_Dialog" parent="android:Theme.Dialog">
        <item name="android:windowBackground">@null</item>
        <item name="android:background">@color/general_background_color</item>
        <item name="android:windowTitleStyle">@style/dialog_title_style</item>
    </style>
    
    <style name="dialog_title_style" parent="android:Widget.TextView">
        <item name="android:textColor">@android:color/black</item>
    </style>
    

     <Spinner
         android:id="@+id/res_spinner"
         android:layout_width="0dp"
         android:layout_height="30dp"
         android:layout_weight="60"
         android:background="@drawable/ref_spinner_selector"
         android:dropDownVerticalOffset="1dp"
         android:dropDownWidth="60dp"
         android:gravity="center"
         android:popupBackground="@null"
         android:spinnerMode="dropdown"/>
    

    how the spinner needs to be look like

    how the spinner looks like now

    由于

    更新: 如果有人有问题来定制微调器

    我设法使用ListPopupWindow解决所有问题

    private void initPopup()
    {
        _lp = new ListPopupWindow(getActivity());
        _lp.setAnchorView(_resTitle);
    
        ColorDrawable cd = new ColorDrawable(getResources().getColor(android.R.color.transparent));
    
        _lp.setBackgroundDrawable(cd);
        _lp.setOnDismissListener(new OnDismissListener()
        {
    
            @Override
            public void onDismiss()
            {
                _resTitle.setSelected(false);
            }
        });
        _listPopupAdapter = new DataSetListAdapter(getActivity(), _resData);
        _lp.setAdapter(_listPopupAdapter);
    }
    

    的onclick

    案例R.id.res_title:

                if (!_lp.isShowing())
                {
                    _resTitle.setSelected(true);
                    _lp.setOnItemClickListener(this);
                    _lp.show();
                    _lp.getListView().setDivider(null);
                    _lp.getListView().setDividerHeight(0);
                }
                else
                {
                    _resTitle.setSelected(false);
                    _lp.dismiss();
                }
                break;
    

1 个答案:

答案 0 :(得分:0)

Android不允许自定义微调器UI.So 如果你想修改你的微调器UI,那么使用android弹出视图,你可以在这里放置你的风格UI,效果如下拉菜单 示例

  spinner=(EditText)findViewById(R.id.txt_Spinner);


    spinner.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            p = new Point();
            p.x = location[0]+(v.getHeight());
            p.y = location[1]+v.getHeight();

            if (p != null)
                showPopup(statusActivity.this, p);

            System.out.println("show popup");
        }
    });




// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
    int popupWidth = 300;
    int popupHeight = 500;

    // Inflate the popup_layout.xml
    LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
    LayoutInflater layoutInflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

    // Creating the PopupWindow
    popup = new PopupWindow(context);
    popup.setContentView(layout);
    popup.setWidth(popupWidth);
    popup.setHeight(popupHeight);
    popup.setFocusable(true);

    // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
    int OFFSET_X = 00;
    int OFFSET_Y = 00;

    // Clear the default translucent background
    popup.setBackgroundDrawable(new BitmapDrawable());

    // Displaying the popup at the specified location, + offsets.
    popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
    ((TextView)layout.findViewById(R.id.textView2)).setClickable(true);
    ((TextView)layout.findViewById(R.id.textView3)).setClickable(true);
    ((TextView)layout.findViewById(R.id.textView4)).setClickable(true);
    ((TextView)layout.findViewById(R.id.textView5)).setClickable(true);
    ((TextView)layout.findViewById(R.id.textView6)).setClickable(true);
    ((TextView)layout.findViewById(R.id.textView7)).setClickable(true);
    ((TextView)layout.findViewById(R.id.textView8)).setClickable(true);
    ((TextView)layout.findViewById(R.id.textView9)).setClickable(true);

}

和popup.xml

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/popup_bg"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    style="@style/text_orange_heading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Select Status"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    style="@style/text_blue_contains"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:onClick="onClick"
    android:clickable="true"
    android:drawableBottom="@drawable/line_white"
    android:tag="Sleeping"
    android:text="Sleeping" />

<TextView
    android:id="@+id/textView3"
    style="@style/text_blue_contains"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:onClick="onClick"
    android:clickable="true"
    android:drawableBottom="@drawable/line_white"
    android:tag="Available"
    android:text="Available" />

<TextView
    android:id="@+id/textView4"
    style="@style/text_blue_contains"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:onClick="onClick"
    android:clickable="true"
    android:drawableBottom="@drawable/line_white"
    android:tag="Busy"
    android:text="Busy" />

<TextView
    android:id="@+id/textView5"
    style="@style/text_blue_contains"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:onClick="onClick"
    android:clickable="true"
    android:drawableBottom="@drawable/line_white"
    android:tag="At work"
    android:text="At work" />

<TextView
    android:id="@+id/textView6"
    style="@style/text_blue_contains"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:onClick="onClick"
    android:clickable="true"
    android:drawableBottom="@drawable/line_white"
    android:tag="Battery charge low"
    android:text="Battery charge low" />

<TextView
    android:id="@+id/textView7"
    style="@style/text_blue_contains"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:onClick="onClick"
    android:clickable="true"
    android:drawableBottom="@drawable/line_white"
    android:tag="In meeting"
    android:text="In meeting" />

<TextView
    android:id="@+id/textView8"
    style="@style/text_blue_contains"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:onClick="onClick"
    android:clickable="true"
    android:drawableBottom="@drawable/line_white"
    android:tag="TMS me later"
    android:text="TMS me later" />

<TextView
    android:id="@+id/textView9"
    style="@style/text_blue_contains"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:onClick="onClick"
    android:clickable="true"
    android:drawableBottom="@drawable/line_white"
    android:tag="At the toilet"
    android:text="At the toilet" />

<EditText
    android:id="@+id/textCustomize"
    style="@style/text_blue_contains"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:tag="Customize"
    android:text="Customize" />