Android 片段内的自定义数字选择器

Android 片段内的自定义数字选择器,android,android-fragments,android-dialog,android-dialogfragment,Android,Android Fragments,Android Dialog,Android Dialogfragment,请帮助我使用来自的NumberPicker。 我有一块碎片,里面有按钮。单击按钮后,我想显示我的号码选择器(作为弹出窗口,而不是新屏幕)。 所以我所做的是: 我创建了NumberPickerCustomDialog public class NumberPickerCustomDialog extends DialogFragment { Context context; @Override public Dialog onCreateDialog(Bundle savedInstanceSt

请帮助我使用来自的
NumberPicker
。 我有一块碎片,里面有按钮。单击按钮后,我想显示我的号码选择器(作为弹出窗口,而不是新屏幕)。 所以我所做的是: 我创建了
NumberPickerCustomDialog

public class NumberPickerCustomDialog extends DialogFragment {
Context context;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // get context
    context = getActivity().getApplicationContext();
    // make dialog object
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // get the layout inflater
    LayoutInflater li = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    // inflate our custom layout for the dialog to a View
    View view = li.inflate(R.layout.activity_dark, null);
    // inform the dialog it has a custom View
    builder.setView(view);
    // and if you need to call some method of the class
    NumberPicker np = (NumberPicker) view
            .findViewById(R.id.numberPicker);

    // create the dialog from the builder then show
    return builder.create();
}
}

现在在我的片段中,单击按钮后,我尝试显示我的数字选择器:

FragmentManager fm = getFragmentManager();
            NumberPickerCustomDialog yourDialog = new NumberPickerCustomDialog();
            yourDialog.show(fm, "some_optional_tag");
<activity android:name=".Activity"
        android:theme="@style/SampleTheme.Light">

</activity>
我的布局看起来:

<?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">

<net.simonvt.numberpicker.NumberPicker
    android:id="@+id/numberPicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

我做错了什么?

好的,我解决了问题。此库需要定义样式,即:

<style name="SampleTheme" parent="android:Theme">
    <item name="numberPickerStyle">@style/NPWidget.Holo.NumberPicker</item>
</style>

<style name="SampleTheme.Light" parent="android:Theme.Light">
    <item name="numberPickerStyle">@style/NPWidget.Holo.Light.NumberPicker</item>
</style>

@style/NPWidget.Holo.NumberPicker
@style/NPWidget.Holo.Light.NumberPicker
在AndroidManifest.xml中,必须为要使用自定义数字选择器的活动定义样式:

FragmentManager fm = getFragmentManager();
            NumberPickerCustomDialog yourDialog = new NumberPickerCustomDialog();
            yourDialog.show(fm, "some_optional_tag");
<activity android:name=".Activity"
        android:theme="@style/SampleTheme.Light">

</activity>


用于对话框的xml布局中存在一些错误。。发布此布局xml文件R.layout.activity\u dark.net.simonvt.numberpicker.numberpicker此包名称可能错误。检查自定义视图的packge名称如果你不介意NumberPicker是我文章中链接中的库,你可以发布你的NumberPicker的代码吗?在没有完整源代码的情况下配置该库有一些问题,我说不出是什么我面临同样的问题,但如果我只想在DialogFragment中设置主题,而不是在活动中,该怎么办,DialogFragment不是一个活动,所以我们不能在Menifest文件中提到它,通过执行您的解决方案,整个活动将获得此主题,而我只想在dialog上使用任何解决方案