0

如何在android中的微调器上放置图像图标请帮助我这是我的微调器代码如何在列表中的每个名称上添加图像?我想在微调器上的 eact 文本上显示图像

            Spinner sp = (Spinner) findViewById(R.id.lgnspinner);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.network_array,
                    android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            sp.setAdapter(adapter);

            sp.setOnItemSelectedListener(new OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    if (position == 0) {
                        // text}

                        if (position == 1) {

                            // text

                        }
                        if (position == 2)

                        {
                            // text
                        }
                        if (position == 3) {
                            // text
                        }
                    }
                }
            });

XML:

<Spinner
    android:id="@+id/lgnspinner"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="145dip"
    android:layout_height="wrap_content"
    android:prompt="@string/network_prompt"
    android:textColor="#000000" />

<string-array name="network_array" >

    <item>
    GPRS
    </item>

    <item>
    Wifi
    </item>

    <item>
    OtherNetwork
    </item>

    <item>
    SMS
    </item>
</string-array>
4

2 回答 2

7

在此处输入图像描述

我们将展示如何使用 SimpleCursorAdapter 和 ViewBinder 将图标添加到 Android 微调器。

// get the spinner widget
mSpinner = (Spinner) findViewById(R.id.spinner1);
...
// fetch data from db. query might look something like this:
// SLECT _id, name FROM ....
mCursor = mDb.getData();
String[] from = new String[]{"_id", "name"};
int[] to = new int[]{R.id.icon, R.id.text};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.spinner_entry, mCursor, from, to);
adapter.setViewBinder(new SpinnerViewBinder());
mSpinner.setAdapter(adapter);

微调器条目的布局 spinner_entry.xml 文件可能如下所示:

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

<LinearLayout ...>

    <ImageView android:id="@+id/icon"   .../>

    <TextView android:id="@+id/text"   .../>

</LinearLayout>

关键在于 SpinnerViewBinder.java

public class SpinnerViewBinder implements SimpleCursorAdapter.ViewBinder {

public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
switch (viewId) {
case R.id.text:
    // the textview
    TextView mView = (TextView) view;
    // display the name
    mView.setText(cursor.getString(columnIndex));
    break;

case R.id.icon:
    // the icon
    ImageView mIconView = (ImageView) view;
    int dialectId = cursor.getInt(columnIndex);
    switch (dialectId){
    case 0:
        mIconView.setImageResource(R.drawable.x1);
        break;
    case 1:
        mIconView.setImageResource(R.drawable.x2);
        break;
    default:
        mIconView.setImageResource(R.drawable.x3);
        break;
    }
}
return true;
}
}

作为参考下载这个简单的应用程序: http ://www.ziddu.com/download/12964268/AndroidCustomSpinner_files_101214a.zip.html

于 2012-10-11T13:18:59.403 回答
4

请参阅:在 android 中自定义微调器。

希望对你有效。

于 2012-10-11T13:10:15.733 回答