Android Spinner与setDropDownViewResource

效果图:

Android Spinner与setDropDownViewResourceAndroid Spinner与setDropDownViewResource

 

第一步:新建Android工程 Test8

 

第二步:在values中添加arrays.xml

 

<resources>
	<string-array name="planets">
		<item>三星</item>
		<item>诺基亚</item>
		<item>BG</item>
		<item>摩托罗拉</item>
		<item>西门子</item>
		<item>联想</item>
		<item>中国电子</item>
	</string-array>
</resources>

 

 

第三步:编辑strings.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, SpinnerDemo!</string>
    <string name="app_name">SpinnerApp</string>
    <string name="planet_prompt">Choose a planet</string>
</resources>

 

 

第四步:编辑main.xml

 

 

 

<Spinner
         android:id="@+id/spinner01"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="10px"
         android:drawSelectorOnTop="true"
         android:prompt="@string/planet_prompt"
         />

 

第五步:编辑:SpinnerDemo.java

 

package com.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;

public class SpinnerDemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Spinner s=(Spinner)findViewById(R.id.spinner01);
        
        ArrayAdapter adapter=ArrayAdapter.createFromResource(this,R.array.planets,android.R.layout.simple_spinner_item);
        
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
        s.setAdapter(adapter);
    }
}

 

 

你可能感兴趣的:(android,xml,OS,三星)