如何在android中为选定值显示透明背景的微调器? package com.android.example; 导入android.app.Activity; 导入android.content.Context; 导入android.graphics.Color; 导入android.os.Bundle; 导入android.view.LayoutInflater; 导入android.view.view; 导入android.view.ViewGroup; 导入android.widget.AdapterView; 导入android.widget.ArrayAdapter; 导入android.widget.BaseAdapter; 导入android.widget.RelativeLayout; 导入android.widget.Spinner; 导入android.widget.TextView; 导入android.widget.Toast; 导入android.widget.AdapterView.OnItemSelectedListener; 公共类SpinerActivity扩展活动实现了MSelectedListener{ 私人住宅; 私有字符串[]mNameList={“一”、“二”、“三”}; 上下文=这个; 旋转器室_型; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.spinner\u activity\u main); 房间类型=(微调器)findViewById(R.id.ics\U微调器); mAdapter=新ICSAdapter(上下文,mNameList); 房间类型设置适配器(mAdapter); 文件室类型。setOnItemSelectedListener(新的OnItemSelectedListener(){ @凌驾 已选择公共视图(适配器视图适配器、视图、, 内部位置,长id){ 房间类型选择(位置); View selectedText=(视图)Adapter.getChildAt(位置); 如果(selectedText!=null){ RelativeLayout real=(RelativeLayout)selectedText.findViewById(R.id.real); TextView selected=(TextView)selectedText.findViewById(R.id.name\u视图); 已选择.setText(mNameList[position].toUpperCase()); ((视图)selected.getParent()).setBackgroundColor(Color.TRANSPARENT); //real.setBackgroundColor(Color.TRANSPARENT); } } @凌驾 未选择公共无效(AdapterView适配器){ //TODO自动生成的方法存根 } }); } 公共类ICSAdapter扩展了BaseAdapter{ 私人充气机; 私人语境; 字符串[]房间类型; 公共ICSAdapter(上下文上下文,字符串[]\u意外识别列表){ this.room_type=_意外事件列表; this.context=上下文; 充气器=(LayoutInflater)this.context .getSystemService(上下文布局\充气机\服务); } @凌驾 public int getCount(){ 返回室类型长度; } @凌驾 公共对象getItem(int位置){ 返回室类型[位置]; } @凌驾 公共长getItemId(int位置){ 返回位置; } @凌驾 公共视图getView(最终整数位置、视图转换视图、视图组父视图){ 视窗座; convertView=充气机。充气(右布局下拉视图,空); holder=新的ViewHolder(); holder.spinnerText=(TextView)convertView.findViewById(R.id.name\u视图); 保持架.喷丝头文本.设置文本(房间类型[位置]); 返回视图; } 公共类视图持有者{ 文本视图喷丝板文本; } } @凌驾 已选择公共视图(适配器视图arg0、视图arg1、内部arg2、, 长arg3){ //TODO自动生成的方法存根 } @凌驾 未选择公共无效(AdapterView arg0){ //TODO自动生成的方法存根 } } activity_main.xml dropdown.xml

如何在android中为选定值显示透明背景的微调器? package com.android.example; 导入android.app.Activity; 导入android.content.Context; 导入android.graphics.Color; 导入android.os.Bundle; 导入android.view.LayoutInflater; 导入android.view.view; 导入android.view.ViewGroup; 导入android.widget.AdapterView; 导入android.widget.ArrayAdapter; 导入android.widget.BaseAdapter; 导入android.widget.RelativeLayout; 导入android.widget.Spinner; 导入android.widget.TextView; 导入android.widget.Toast; 导入android.widget.AdapterView.OnItemSelectedListener; 公共类SpinerActivity扩展活动实现了MSelectedListener{ 私人住宅; 私有字符串[]mNameList={“一”、“二”、“三”}; 上下文=这个; 旋转器室_型; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.spinner\u activity\u main); 房间类型=(微调器)findViewById(R.id.ics\U微调器); mAdapter=新ICSAdapter(上下文,mNameList); 房间类型设置适配器(mAdapter); 文件室类型。setOnItemSelectedListener(新的OnItemSelectedListener(){ @凌驾 已选择公共视图(适配器视图适配器、视图、, 内部位置,长id){ 房间类型选择(位置); View selectedText=(视图)Adapter.getChildAt(位置); 如果(selectedText!=null){ RelativeLayout real=(RelativeLayout)selectedText.findViewById(R.id.real); TextView selected=(TextView)selectedText.findViewById(R.id.name\u视图); 已选择.setText(mNameList[position].toUpperCase()); ((视图)selected.getParent()).setBackgroundColor(Color.TRANSPARENT); //real.setBackgroundColor(Color.TRANSPARENT); } } @凌驾 未选择公共无效(AdapterView适配器){ //TODO自动生成的方法存根 } }); } 公共类ICSAdapter扩展了BaseAdapter{ 私人充气机; 私人语境; 字符串[]房间类型; 公共ICSAdapter(上下文上下文,字符串[]\u意外识别列表){ this.room_type=_意外事件列表; this.context=上下文; 充气器=(LayoutInflater)this.context .getSystemService(上下文布局\充气机\服务); } @凌驾 public int getCount(){ 返回室类型长度; } @凌驾 公共对象getItem(int位置){ 返回室类型[位置]; } @凌驾 公共长getItemId(int位置){ 返回位置; } @凌驾 公共视图getView(最终整数位置、视图转换视图、视图组父视图){ 视窗座; convertView=充气机。充气(右布局下拉视图,空); holder=新的ViewHolder(); holder.spinnerText=(TextView)convertView.findViewById(R.id.name\u视图); 保持架.喷丝头文本.设置文本(房间类型[位置]); 返回视图; } 公共类视图持有者{ 文本视图喷丝板文本; } } @凌驾 已选择公共视图(适配器视图arg0、视图arg1、内部arg2、, 长arg3){ //TODO自动生成的方法存根 } @凌驾 未选择公共无效(AdapterView arg0){ //TODO自动生成的方法存根 } } activity_main.xml dropdown.xml,android,android-spinner,dropdownlistfor,Android,Android Spinner,Dropdownlistfor,在这里,我使用一些背景图像的默认微调器。我有一个问题,当我单击微调器时,下拉列表将以白色背景显示。但原始颜色背景将不显示(android:background=“#2D333C”) 我正在使用透明颜色来更改背景。如何解决此问题。当我单击下拉列表中的微调器时,我想显示给定的背景颜色。选择所选值后,背景将变为父级。有人能帮我解决此问题吗?在drawable文件夹中创建一个自定义文件 mybackground.xml package com.android.example; import androi

在这里,我使用一些背景图像的默认微调器。我有一个问题,当我单击微调器时,下拉列表将以白色背景显示。但原始颜色背景将不显示(android:background=“#2D333C”)
我正在使用透明颜色来更改背景。如何解决此问题。当我单击下拉列表中的微调器时,我想显示给定的背景颜色。选择所选值后,背景将变为父级。有人能帮我解决此问题吗?

在drawable文件夹中创建一个自定义文件

mybackground.xml

package com.android.example;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class SpinnerActivity extends Activity implements OnItemSelectedListener {

    private ICSAdapter mAdapter;
    private String[] mNameList = {"one","two","three" };
    Context context=this;
    Spinner room_type;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner_activity_main);
        room_type = (Spinner) findViewById(R.id.ics_spinner);

        mAdapter = new ICSAdapter(context, mNameList);
         room_type.setAdapter(mAdapter);
         room_type.setOnItemSelectedListener(new OnItemSelectedListener() {
           @Override
                public void onItemSelected(AdapterView<?> Adapter, View view,
                        int position, long id) {
                        room_type.setSelection(position);
                        View selectedText = (View) Adapter.getChildAt(position);
                        if (selectedText != null) {
                            RelativeLayout real = (RelativeLayout) selectedText.findViewById(R.id.real);
                            TextView selected = (TextView) selectedText.findViewById(R.id.name_view);
                            selected.setText(mNameList[position].toUpperCase());
                            ((View) selected.getParent()).setBackgroundColor(Color.TRANSPARENT);
//                          real.setBackgroundColor(Color.TRANSPARENT);

                        }
                    }
                    @Override
                public void onNothingSelected(AdapterView<?> Adapter) {
                    // TODO Auto-generated method stub

                }
            });

    }

    public class ICSAdapter extends BaseAdapter {

        private LayoutInflater inflater;
        private Context context;
        String[] room_type;

        public ICSAdapter(Context context, String[] _accidentList) {
            this.room_type = _accidentList;
            this.context = context;

            inflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return room_type.length;
        }

        @Override
        public Object getItem(int position) {
            return room_type[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            convertView = inflater.inflate(R.layout.drop_down_view, null);
            holder = new ViewHolder();
            holder.spinnerText   = (TextView) convertView.findViewById(R.id.name_view);
            holder.spinnerText.setText(room_type[position]);
           return convertView;
        }


        public class ViewHolder {
            TextView spinnerText;
        }

    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
}

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@drawable/bg"
    android:layout_gravity="center"
    >

<Spinner
        android:id="@+id/ics_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/text3"
       />

</RelativeLayout>

dropdown.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/real"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#2D333C" >

    <TextView
        android:id="@+id/name_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:text="324"
        android:textColor="@color/android:white"
        android:textSize="18sp" />

</RelativeLayout>

现在将此文件设置为微调器

<?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="false" android:drawable="@color/yourcolorName" />
  <item android:drawable="@android:color/transparent" />
</selector>


Try((RelativeLayout)视图).setbackgroundcolor(此处颜色透明);在选择的方法中。执行此操作之前请检查是否为null检查此项:SetPopuBackground&setBackground方法我尝试了以下((RelativeLayout)视图)。setbackgroundcolor(此处颜色透明)。但什么也没有发生。你试过了吗?你在values文件夹中创建了彩色xml文件了吗?当我单击微调器时,下拉列表显示白色背景。尝试将android:state_pressed=“false”更改为android:state_focused=“true”不出现。实际上问题是下拉列表显示为白色背景。你尝试过吗?
<Spinner
    android:id="@+id/ics_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/mybackground"
   />