我对微调器设计有问题。我正在使用以下代码生成下拉菜单:
@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);
}
如何去除阻塞微调器的黑色?
答案 0 :(得分:2)
layout.addView(spinner);
setContentView(layout);
将视图动态添加到布局时。您缺少一些配置。这就是您看到黑匣子的原因。
尝试以下代码:
xml:
<Spinner
android:id="@+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
活动:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
arraydata, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
答案 1 :(得分:1)
在布局文件夹中创建一个布局文件simple_list.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:text="Sample Text"
android:padding="5dp"
android:gravity="center"
android:textColor="@android:color/black"
android:background="@android:color/white"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"/>
并在arrayadapter中引用它:
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
R.layout.simple_list, spinnerArray);
更新1 添加此内容:
spinner.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),android.R.color.white));
更新2 不用使用约束,只需在布局文件中使用Linear:
<?xml version="1.0" encoding="utf-8"?>
<LineartLayout xmlns:android="schemas.android.com/apk/res/android" xmlns:app="schemas.android.com/apk/res-auto"
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,
android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
layout.addView(spinner);
setContentView(layout);
答案 2 :(得分:1)
答案 3 :(得分:0)
在您的activity_my布局中添加一个linearlayout并将其命名为linearLayoutContainer。然后从代码中获取对它的引用。
LinearLayout container = findViewById(R.id.linearLayoutContainer);
Spinner spinner = new Spinner(this);
container.addView(spinner);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);