1,如果仅仅是用于数字 NumberPicker 更为方便
NumberPicker :
<com.shawnlin.numberpicker.NumberPicker
android:layout_gravity="center_horizontal"
android:id="@+id/picker_height"
android:layout_width="200dp"
android:layout_height="250dp"
android:layout_centerInParent="true"
app:np_width="64dp"
app:np_height="180dp"
app:np_dividerColor="@color/colorPrimary"
app:np_formatter="@string/number_picker_formatter"
app:np_max="240"
app:np_min="90"
app:np_value="160"
app:np_wheelItemCount="5"
app:np_wrapSelectorWheel="false"
app:np_selectedTextColor="@color/colorPrimary"
app:np_selectedTextSize="@dimen/selected_text_size"
app:np_textColor="@color/colorPrimary"
app:np_textSize="@dimen/text_size"
app:np_typeface="@string/roboto_light"
/>
private void setNumberPickerDividerColor(NumberPicker numberPicker) {
Field[] pickerFields = NumberPicker.class.getDeclaredFields();
for (Field pf : pickerFields) {
if (pf.getName().equals("mSelectionDivider")) {
pf.setAccessible(true);
try {
//设置分割线的颜色值
pf.set(numberPicker, new ColorDrawable(getResources().getColor(R.color.numberpicker_divider_color)));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
}
<cn.carbswang.android.numberpickerview.library.NumberPickerView
android:id="@+id/picker"
android:layout_width="wrap_content"
android:layout_height="240dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="#ffffff"
android:contentDescription="test_number_picker_view"
app:npv_ItemPaddingHorizontal="5dp"
app:npv_ItemPaddingVertical="5dp"
app:npv_ShowCount="5"
app:npv_RespondChangeOnDetached="false"
app:npv_TextSizeNormal="16sp"
app:npv_TextSizeSelected="20sp"
app:npv_WrapSelectorWheel="true"/>
NumberPickerView picker = (NumberPickerView) findViewById(R.id.picker);
String[] display=new String[150];
for(int i=90;i<240;i++){
display[i-90]=i+"";
}
picker.refreshByNewDisplayedValues(display);