1、Activity 实现方法
public class AboutDeviceActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private String[] starArray={"A","B","C"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_device);
initView();
}
private void initView() {
Spinner mLogcatSpinner = findViewById(R.id.logcat_spinner);
//1、 starArray ,动态设置
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.module_spinner_item_text, starArray);
// 2、res/values/arrays.xml
//系统样式 item R.layout.support_simple_spinner_dropdown_item
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.logcat, R.layout.support_simple_spinner_dropdown_item);
//自定义 item 样式 R.layout.module_spinner_item_text
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.logcat, R.layout.module_spinner_item_text);
//设置
mLogcatSpinner.setAdapter(adapter);
mLogcatSpinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) {
case R.id.logcat_spinner:
String itemName = parent.getItemAtPosition(position).toString();
Log.d(TAG, "logcat_spinner=" + itemName);
break;
default:
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
spinner:
<Spinner
android:id="@+id/security_module_spinner"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2.5"
android:background="@color/white"
android:dropDownVerticalOffset="60dp"
android:dropDownSelector="#0352D4"
android:entries="@array/logcat"
android:orientation="horizontal" />
1、android:entries="@array/SecurityMode"
res/values 下创建 arrays.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="logcat">
<item>Password</item>
<item>Disable</item>
</string-array>
</resources>
2、module_spinner_item_text.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="60dp"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center"
android:textAlignment="inherit"
android:textStyle="bold"
android:textSize="22dp">
</TextView>
|