问题描述
我想配置使用的 API 14 的 NumberPicker
小部件。有很少的教程或说明网上说会帮我设置正确。
I am trying to configure the NumberPicker
widget using API 14. There are very little tutorials or instructions online that would help me set it up properly.
到目前为止,我已经在实行以下code的的main.xml 的:
so far I have the following code implemented in main.xml:
<NumberPicker
android:id="@+id/numberPicker1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal" />
和在onCreate()方法的主要活动:
and in the main activity in the onCreate() method:
NumberPicker np = (NumberPicker) findViewById(R.id.numberPicker1);
String[] nums = new String[20];
for(int i=0; i<nums.length; i++)
nums[i] = Integer.toString(i);
np.setMinValue(1);
np.setMaxValue(20);
np.setWrapSelectorWheel(false);
np.setDisplayedValues(nums);
np.setValue(1);
在运行我的应用程序在模拟器如预期的那样numberpicker部件没有得到显示。按钮的不左,右,但向上和向下。和值覆盖,而不是显示一个号码对方。点击箭头的应用程序崩溃。
When running my application in the emulator the numberpicker widget does not get displayed as expected. The button's are not left and right but up and down. And the values overlay each other instead of one number being displayed. Clicking an arrow crashes the application.
修改
在第一次的答案下面介绍然而numberpciker不可用,请见截图:所做的更改
Made changes as described below in first answer however the numberpciker is not usable, please see screenshot:
任何帮助将是AP preciated。
Any help would be appreciated.
推荐答案
尝试注释掉for循环和行 np.setDisplayedValues(NUMS);
。在 NumberPicker
应该为你自动显示的值。
Try commenting out the for loop and the line np.setDisplayedValues(nums);
. The NumberPicker
should display the values for you automatically.
您也可能会考虑更改宽度的android:layout_width =WRAP_CONTENT
如果你想在 NumberPicker
来水平显示你的价值观。
You might also consider changing the width to android:layout_width="wrap_content"
if you want the NumberPicker
to display your values horizontally.
看来,你不能改变 NumberPicker
(请参阅本链接)。问题是,你正在试图改变安卓方向
属性...但是这是从的LinearLayout $ C $继承属性C>类。你可能会坚持与垂直显示
NumberPicker
除非你自定义自己。对不起!
It appears that you cannot change NumberPicker
(see this link). The issue is that you are trying to change the android:orientation
attribute... but this is an inherited attribute from the LinearLayout
class. You might be stuck with a vertically displaying NumberPicker
unless you customize your own. Sorry!
这篇关于如何使用NumberPicker小部件的Android 4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!