【问题标题】:How to custom spinner?如何自定义微调器?
【发布时间】:2016-07-05 12:31:47
【问题描述】:

我想在 android 中自定义微调器,如下所述 - 占位符文本(微调器第一项 - 选定的项目将居中) - 下拉列表项将左对齐

我尝试为 Spinner 使用自定义布局适配器,但仅对齐微调器和下拉列表。 我希望有人可以帮助我!我是 Android 新手,对此感到抱歉!

【问题讨论】:

  • 请提供您的代码。

标签: android spinner customization


【解决方案1】:

这是我最近在应用中使用的一个示例。

在您的 res/layout 文件夹中创建 2 个文件,一个名为 spinner_list.xml,另一个名为 spinner_list_dropdown.xml。

spinner_list.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/text_size"
    android:padding="10dp"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:textColor="@color/text_view_text"/>

spinner_list_dropdown.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/text_size"
    android:padding="10dp"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:textColor="@color/text_view_text"
    android:background="@color/spinner_background"/>

使用自定义微调器的 Java 代码:

 ArrayAdapter<String> adapter = new ArrayAdapter<>(YourActivity.this, R.layout.spinner_list, strYourArray);
        adapter.setDropDownViewResource(R.layout.spinner_list_dropdown);
        spinnerCustomList.setAdapter(adapter);

希望这一切对您有意义,如果您需要任何帮助,请不要犹豫。

【讨论】:

  • 谢谢,你有微调器多布局的例子吗?
  • @EddyLee 这对你有帮助吗?
猜你喜欢
  • 2014-03-08
  • 1970-01-01
  • 2018-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多