--------------------MainActivity--------------------
package com.example.example3_2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
final static int WRAP_CONTENT=-2;
int []image={R.mipmap.basketball,R.mipmap.football,R.mipmap.volleyball};
int []msgid={R.string.zuqiu,R.string.lanqiu,R.string.pingpangqiu};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner=(Spinner)this.findViewById(R.id.spinner);
BaseAdapter baseAdapter=new BaseAdapter() { //设置适配器
@Override
public int getCount() {
return 3; //总共三项
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout linearLayout=new LinearLayout(MainActivity.this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView imageView=new ImageView(MainActivity.this);
imageView.setImageDrawable(getResources().getDrawable(image[position]));
linearLayout.addView(imageView); //添加到LinearLayout
TextView textView=new TextView(MainActivity.this);
textView.setText(""+getResources().getText(msgid[position])); //设置内容
textView.setTextSize(24);
textView.setTextColor(getResources().getColor(R.color.colorPrimary)); //设置字体颜色
linearLayout.addView(textView); //添加到LinearLayout
return linearLayout;
}
};
spinner.setAdapter(baseAdapter);
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView=(TextView)findViewById(R.id.text_view);
LinearLayout linearLayout=(LinearLayout)view; //获取选中的Linearlayout
TextView textView1=(TextView)linearLayout.getChildAt(1); //获取其中的TextView
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append(getResources().getText(R.string.ys));
stringBuilder.append(":");
stringBuilder.append(textView1.getText());
textView.setText(stringBuilder.toString());
}
public void onNothingSelected(AdapterView<?>parent){} //重写的方法
});
}
}
------------------------main.XML---------------------
<?xml version="1.0" encoding="utf-8"?>
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.example3_2.MainActivity">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ys" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Spinner>
------------------------string.XML-------------------
<?xml version="1.0" encoding="utf-8"?>
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.example3_2.MainActivity">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ys" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Spinner>
--------------------color.XML--------------------------
<?xml version="1.0" encoding="utf-8"?>
#3F51B5
#303F9F
#FF4081