如果您想创建自己的微调器,请快速(并且效率低下)示例。 (以列表视图适配器工作):
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<FrameLayout android:id="@+id/container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<Button android:id="@+id/btn_up"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="up"/>
<Button android:id="@+id/btn_down"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="down"/>
</LinearLayout>
</LinearLayout>
CustomSpinner类:
public class CustomSpinner extends FrameLayout{
// ------------------------------ FIELDS ------------------------------
private FrameLayout mContainer;
private SpinnerAdapter mAdapter;
private int index = 0;
// --------------------------- CONSTRUCTORS ---------------------------
public CustomSpinner(Context context) {
super(context);
init(context);
}
public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context){
this.addView(LayoutInflater.from(context).inflate(R.layout.custom_spinner,this,false));
findViewById(R.id.btn_up).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
index--;
refresh();
}
});
findViewById(R.id.btn_down).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
index++;
refresh();
}
});
mContainer = (FrameLayout) findViewById(R.id.container);
}
// -------------------------- OTHER METHODS --------------------------
public void setAdapter(SpinnerAdapter adapter) {
this.mAdapter = adapter;
refresh();
}
private void refresh() {
//----needs recycling for better performance---
//---now, we'll just clear up--
mContainer.removeAllViews();
//---do we need to refresh ? -----
if(mAdapter == null || mAdapter.getCount() == 0){return;}
//--clamp index--
index = Math.max(0,index);
index = Math.min(mAdapter.getCount() - 1, index);
//--get view and show it-----
View currentView = mAdapter.getView(index,null,mContainer);
mContainer.addView(currentView);
}
}
使用案例:
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<com.example.CustomSpinner android:id="@+id/custom_spinner"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
</LinearLayout>
活动:
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
new String[]{"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"});
((CustomSpinner)findViewById(R.id.custom_spinner)).setAdapter(adapter);
}
}
您可以将一个'TextView'和两个'ImageButton'与一个项目列表组合成一个传统的微调器。 –
我建议创建一个窄的ListView。如果您在顶部和底部添加渐变到黑色的渐变效果,它看起来就像基本的Android [时钟或计时器UI](https://encrypted-tbn0.google.com/images?q=tbn:ANd9GcRlB3VzygBVwe0xmxp9M8yFPNbqH1GgjqKvW5BoWllQd9mqzrzcAQ) 。 (这个图像有三个支持甩手的“轮子”,圆形效果是一种光学幻觉)。如果你不想从头开始编程,我确信有可以购买的包装已经有了这个。 – Sam
好吧,使用文本而不是数字填充NumberPicker并不是太棘手, [看看这里](http://stackoverflow.com/questions/8227073/using-numberpicker-widget-with-strings)。如果你四处搜索,我相信你也能找到提供类似功能的定制组件。 –