我正在使用微调器显示月份列表。
但 我想将图像设置为覆盖月份列表?但我没有找到任何精确的解决方案。
答案 0 :(得分:1)
Spinner.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true" />
<ImageView android:id="@+id/myimgview" android:layout_width="fill_parent" android:layout_height="fill_parent"></ImageView>
</LinearLayout>
您的活动类必须如下所示:
package my.co.home;
import android.app.Activity;
import android.content.Intent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class ViewsActivity extends Activity
{
String[] presidents = {
"January",
"February",
"March",
"December"
};
Spinner s1;
ImageView ivl;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//startActivity(new Intent(this, SpinnerActivity.class));
s1 = (Spinner) findViewById(R.id.spinner1);
ivl=(ImageView) findViewById(R.id.myimgview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, presidents);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3)
{
int index = s1.getSelectedItemPosition();
//Depending on index you display the image..For example i have taken for //two indexes
if(index==1)
ivl.setBackgroundDrawable(getResources().getDrawable(R.drawable.images));
else if(index==2)
ivl.setBackgroundDrawable(getResources().getDrawable(R.drawable.images1));
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
}