Spinner首个项目分隔符 Android

huangapple go评论83阅读模式
英文:

Spinner First Item Devider android

问题

我在我的Android应用程序中使用了类似下面的Spinner:

spino = dialog.findViewById(R.id.language_spinner);

if (spino != null) {
    spino.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String item_position = String.valueOf(position);
            int itemposition = Integer.parseInt(item_position);
            String SelectedLang = String.valueOf(spino.getAdapter().getItem(position));
            viewModel.SelectedLangPosition = itemposition;
            switch (SelectedLang) {
                case "English":
                    viewModel.SelectedLang = "en";
                    break;
                case "ગુજરાતી":
                    viewModel.SelectedLang = "gu";
                    break;
                case "हिंदी":
                    viewModel.SelectedLang = "hi";
                    break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

它按预期工作,但我想为我的第一个标题项"choose video lanaguage"添加视图分隔符,我不知道该如何实现。我的Spinner当前的外观如下:

Spinner首个项目分隔符 Android

谢谢!

英文:

I am using spinner in my android application like below

   spino = dialog.findViewById(R.id.language_spinner);

        if (spino != null) {
            spino.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                    String item_position = String.valueOf(position);
                    int itemposition = Integer.parseInt(item_position);
                   String SelectedLang = String.valueOf(spino.getAdapter().getItem(position));
                    viewModel.SelectedLangPosition = itemposition;
                    switch (SelectedLang) {
                        case &quot;English&quot;:
                            viewModel.SelectedLang = &quot;en&quot;;
                            break;
                        case &quot;ગુજરાતી&quot;:
                            viewModel.SelectedLang = &quot;gu&quot;;
                            break;
                        case &quot;हिंदी&quot;:
                            viewModel.SelectedLang = &quot;hi&quot;;
                            break;
                    }

                }

                @Override
                public void onNothingSelected(AdapterView&lt;?&gt; parent) {

                }
            });
        }

Its working fine as expected, but I am looking for make view divider for my first title item called choose video lanaguage, I am not getting idea how I can do it. my current look of spinner is like below

Spinner首个项目分隔符 Android

Thanks

答案1

得分: 0

第一种方法是将以下两行代码添加到您的Spinner XML 中:

android:spinnerMode="dialog"
android:prompt="@string/promptMsg"

其中 "promptMsg" 被设置为 "选择语言"。或者您可以在代码中以编程方式设置标题消息:

保持 spinnerMode 设置为 dialog,并在您的 Java 代码中添加以下内容:

spinner.setPrompt("选择语言");
英文:

Two ways you can do this.

The first way is to add these two lines of code to your Spinner XML:

android:spinnerMode=&quot;dialog&quot;
android:prompt=&quot;@string/promptMsg&quot;

promtMsg being "Select Language". Or you can set your title message programmatically:

Keep spinnerMode set to dialog and add this to your java code

spinner.setPrompt( &quot;Select language&quot;);

huangapple
  • 本文由 发表于 2020年8月9日 09:47:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63321759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定