xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_match_parent"
android:layout_match_parent"
tools:cOntext="com.example.mywillstudy.MainActivity">
<ToggleButton
android:layout_wrap_content"
android:layout_wrap_content"
android:textOff="开"
android:textOn="关"
android:id="@+id/tob"
android:layout_marginLeft="53dp"
android:layout_marginStart="53dp"
android:layout_marginTop="90dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Spinner
android:layout_wrap_content"
android:layout_wrap_content"
android:id="@+id/ss"
android:layout_alignTop="@+id/tob"
android:layout_toRightOf="@+id/tob"
android:layout_toEndOf="@+id/tob"
android:layout_marginLeft="72dp"
android:layout_marginStart="72dp">Spinner>
<ProgressBar
color:#008000;font-weight:bold">"?android:attr/progressBarStyleHorizontal"
android:layout_200dp"
android:layout_wrap_content"
android:id="@+id/f1"
android:layout_marginTop="46dp"
android:layout_below="@+id/tob"
android:layout_alignLeft="@+id/tob"
android:layout_alignStart="@+id/tob"
android:layout_marginLeft="17dp"
android:layout_marginStart="17dp" />
RelativeLayout>
<resources>
<string name="app_name">MyWillStudystring>
<string-array name="spinner">
<item>北京item>
<item>福建item>
<item>莆田item>
<item>中国item>
string-array>
resources>
package com.example.mywillstudy;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ToggleButton toggleButton = (ToggleButton) this.findViewById(R.id.tob);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
toggleButton.setChecked(b);
}
});
Spinner spinner = (Spinner)this.findViewById(R.id.ss);
String[] item = getResources().getStringArray(R.array.spinner);
ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,item);
spinner.setAdapter(arrayAdapter);
}
}