Spinner 显示黑色

我有一个关于微调器设计的问题。我正在使用此代码生成下拉微调器:


@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_my);


    LinearLayout layout = new LinearLayout(this);


    ArrayList < String > spinnerArray = new ArrayList < String > ();

    spinnerArray.add("one");

    spinnerArray.add("two");

    spinnerArray.add("three");

    spinnerArray.add("four");

    spinnerArray.add("five");


    Spinner spinner = new Spinner(this);

    ArrayAdapter < String > spinnerArrayAdapter = new ArrayAdapter < String > (this,

        android.R.layout.simple_spinner_dropdown_item, spinnerArray);

    spinner.setAdapter(spinnerArrayAdapter);


    layout.addView(spinner);


    setContentView(layout);

}

http://img.mukewang.com/618ccafc00017ffa05000892.jpg如何去除阻挡微调器的黑色?

牧羊人nacy
浏览 319回答 3
3回答

翻阅古今

layout.addView(spinner);setContentView(layout);当您将视图动态添加到布局时。你缺少一些配置。这就是您看到黑匣子的原因。试试下面的代码:xml:<Spinner&nbsp; &nbsp; android:id="@+id/planets_spinner"&nbsp; &nbsp; android:layout_width="fill_parent"&nbsp; &nbsp; android:layout_height="wrap_content" />活动:Spinner spinner = (Spinner) findViewById(R.id.spinner);&nbsp; &nbsp; // Create an ArrayAdapter using the string array and a default spinner layout&nbsp; &nbsp; ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arraydata, android.R.layout.simple_spinner_item);&nbsp; &nbsp; // Specify the layout to use when the list of choices appears&nbsp; &nbsp; adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);&nbsp; &nbsp; // Apply the adapter to the spinner&nbsp; &nbsp; spinner.setAdapter(adapter);

四季花海

在您的布局文件夹中创建一个布局文件 simple_list.xml:<TextView xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; android:text="Sample Text"&nbsp; &nbsp; android:padding="5dp"&nbsp; &nbsp; android:gravity="center"&nbsp; &nbsp; android:textColor="@android:color/black"&nbsp; &nbsp; android:background="@android:color/white"&nbsp; &nbsp; android:orientation="vertical" android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="wrap_content"/>并在 arrayadapter 中引用它:ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; R.layout.simple_list, spinnerArray);更新 1添加这个:spinner.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),android.R.color.white));更新 2 在布局文件中使用 Linear 一次而不是使用约束:&nbsp;<?xml version="1.0" encoding="utf-8"?>&nbsp;&nbsp; &nbsp; <LineartLayout xmlns:android="schemas.android.com/apk/res/android" xmlns:app="schemas.android.com/apk/res-auto"&nbsp;android:id="@+id/linearLayout"xmlns:tools="schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> </LinearLayout>将 id 设置为 LinearLayout(您的根视图),然后findViewById用于此视图并将微调器添加到此 rootView 并删除setContentView(layout); 它,如下所示:LinearLayout layout = new LinearLayout(this);ArrayList<String> spinnerArray = new ArrayList<String>();spinnerArray.add("one");spinnerArray.add("two");spinnerArray.add("three");spinnerArray.add("four");spinnerArray.add("five");Spinner spinner = new Spinner(this);ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,&nbsp;android.R.layout.simple_spinner_dropdown_item, spinnerArray);spinner.setAdapter(spinnerArrayAdapter);layout.addView(spinner);setContentView(layout);

精慕HU

有趣的是你的代码对我有用。因此,请确保那里没有元素或其他与主题或颜色相关的代码。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java