【问题标题】:Custom row in SpinnerSpinner 中的自定义行
【发布时间】:2013-04-27 11:40:40
【问题描述】:

我只需要 Spinner 行中的项目在它们的长度很大时进行换行。 所以我这样做了:

item.xml

<TextView
    android:id="@+id/tvAddr"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:text="Address"
    android:textSize="14sp" />

使用 Spinner 布局:

...

   <Spinner
        android:id="@+id/spAddr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

...

以及实现数据的代码

Cursor c = tbl.getAddr(id);

    getActivity().startManagingCursor(c);
    String[] from = new String[] { "address" };
    int[] to = new int[] { android.R.id.text1 };
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_spinner_item, c,
            from, to);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Spinner s = (Spinner) v.findViewById(R.id.spAddr);
    s.setAdapter(adapter);

但我不知道如何在这里使用我的 row-resource item.xml。如何实现我的自定义行?

UPD:在我用 MSquare 代码更改 item.xml 之后。

封闭式微调器

打开的微调器

当我选择项目时,我又看到了图片 1。

【问题讨论】:

  • 创建自定义适配器以及行的自定义布局。
  • 基于 SimpleCursorAdapter?像在 ListView 中一样?

标签: java android spinner customization


【解决方案1】:

试试这个:

你的 item.xml 应该是这样的:

<?xml version="1.0" encoding="utf-8"?>

<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:text="Address"
    android:textSize="14sp" />

在你现在可以使用的代码中:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), 
        R.layout.item, c, from, to);

试试看。

【讨论】:

  • 它不能正常工作。它在关闭时显示文本地址,项目没有按我想要的方式包装,当我选择一个项目时,微调器中的文本不会改变,它总是显示“地址”。
  • 我可能误解了你的问题。你是什​​么意思“当它关闭时”?你的问题也没有提到选择一个项目?您在某处有 onItemClickListener 吗?发布代码并尝试解释您要做什么。
  • 在item.xml中,尝试去掉android:text="Address",在TextView中添加android:singleLine="false"。
  • 不幸的是它没有改变任何东西:(
【解决方案2】:

为时已晚,但也许对某人有帮助 这是我的自定义微调器代码

final CardDAO cardDAO = new CardDAO(activity);
final Cursor cursor =  cardDAO.getNameAllActiveCard();

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(activity,
                R.layout.spinner_row,
                cursor, new String[] {"pan", "name"}, new int[] {R.id.spinner_pan, R.id.spinner_name}, 0);

        adapter.setDropDownViewResource(R.layout.spinner_dropdown);
Spinner spinner = (Spinner) view.findViewById(R.id.operCardList);
spinner.setAdapter(adapter);

spinner_row.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/spinner_pan"
   android:paddingRight="5dp"
   android:textSize="13sp" /> 

spinner_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:id="@+id/spinner_name"
    android:padding="7dp"
    android:textSize="16sp" />

现在您可以根据需要自定义它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 2011-02-12
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多