spinner就是能夠點擊的TextView,徹底能夠使用TextView,使用適當的背景,作出下拉框,其實更方便,若是使用spinner這個組件,能夠分別定義,寫了個Demo,spinner很差看,能夠換背景圖片,代碼以下: android
1.首先AndroidManifest.xml app
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testspinnertext"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" /> ide
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:anyDensity="true"/> 佈局
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest> 字體
2.res目錄下,創建分別創建layout(原來就已經存在的)和layout-large,這個兩個文件夾下的文件實際上是同樣的,只是要改的佈局和尺寸等 this
1).layout中的文件 spa
activity_main.xml中: orm
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="@string/hello_world"/>
</LinearLayout> xml
spinner_style.xml中 圖片
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:padding="10dip"
android:id="@+id/cit_spinner_item"
android:textSize="15dp">
</TextView>
2).layout-large.xml中的文件
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="@string/hello_world"/>
</LinearLayout>
spinner_style.xml中
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:padding="10dip"
android:id="@+id/cit_spinner_item"
android:textSize="25dp">
</TextView>
3.src目錄下,要創建2個不一樣的適配器,具體代碼以下:
1)MJSimpleSpinnerAdapter類中
package com.example.testspinnertext;
複製到這裏的面板後,格式好難看。
2).MJLargeSpinnerAdapter類中
package com.example.testspinnertext;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MJLargeSpinnerAdapter extends ArrayAdapter<String>{
/**
* @param args
*/
Context context;
String[] items = new String[]{};
public MJLargeSpinnerAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.context = context;
this.items = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(android.R.layout.simple_spinner_item, parent,false);
}
TextView text = (TextView)convertView.findViewById(android.R.id.text1);
text.setText(items[position]);
text.setTextColor(Color.RED);
text.setTextSize(25);
return convertView;
}
}
3).MainActivity類中
package com.example.testspinnertext;
import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Spinner;
public class MainActivity extends Activity {
String[] str = {"選項一","選項二","選項三","選項四"};
Spinner spinner;
private int width = 0;
private int height = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spinner);
//根據屏幕的寬和高來決定spinner中顯示的字體的樣式等(不是spinner的下拉框中的字體)
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
width = dm.widthPixels;
height = dm.heightPixels;
Log.i("寬高", width+","+height);
// MJSimpleSpinnerAdapter adapter = new MJSimpleSpinnerAdapter(MainActivity.this,
// android.R.layout.simple_spinner_item, str);
// spinner.setAdapter(adapter);
if(width<=500&&height<=800){
MJSimpleSpinnerAdapter adapter = new MJSimpleSpinnerAdapter(MainActivity.this,
android.R.layout.simple_spinner_item, str);
adapter.setDropDownViewResource(R.layout.spinner_style);
spinner.setAdapter(adapter);
}else {
MJLargeSpinnerAdapter adapter = new MJLargeSpinnerAdapter(MainActivity.this,
android.R.layout.simple_spinner_item, str);
adapter.setDropDownViewResource(R.layout.spinner_style);
spinner.setAdapter(adapter);
}
}
}