【问题标题】:Why is the 'prompt' of the android spinner not shown?为什么没有显示 android spinner 的“提示”?
【发布时间】:2016-06-28 14:05:45
【问题描述】:

在android中我定义了一个布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:columnCount="4"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar_setting"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

    <Spinner
      android:id="@+id/settings_interval"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:prompt="@string/spinner_title"/>

      ...

但由于某种原因,当我启动相应的活动时,微调器的 android:promp 文本未显示。为了完整起见,这里是该活动:

public class SettingsActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

     @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.settings);

         // Set toolbar, allow going back.
         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_setting);
         //toolbar.setDisplayHomeAsUpEnabled(true);
         //toolbar.setTitle("Settings");
         setSupportActionBar(toolbar);
         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
         getSupportActionBar().setTitle("Settings");

         Spinner spinner = (Spinner) findViewById(R.id.settings_interval);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.listValues, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);    
     }
}

【问题讨论】:

  • 跟我遇到的问题一模一样!!
  • 您使用的是什么微调模式?对话框还是下拉菜单??
  • 将该行粘贴到微调器标签并检查 android:spinnerMode="dialog"
  • 我猜是下拉菜单?请看代码...
  • 默认为下拉模式。将其更改为对话框,然后将显示提示。

标签: android


【解决方案1】:

这是来自Spinner类的代码:

public void setPromptText(CharSequence hintText) {
    // Hint text is ignored for dropdowns, but maintain it here.
    mHintText = hintText;
}

看起来像下拉模式下的微调器忽略提示。尝试设置android:spinnerMode="dialog"进行检查。

你也可以看看here来解决这个问题。

【讨论】:

    猜你喜欢
    • 2017-05-28
    • 2011-09-19
    • 2012-03-14
    • 1970-01-01
    • 2011-05-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    相关资源
    最近更新 更多