【发布时间】:2013-06-11 20:22:53
【问题描述】:
我需要创建一个只有三个值的下拉菜单。我已经使用微调器完成了这项工作。不过貌似This
但我正在尝试创建像This
所以当点击它时,它需要像普通微调器一样显示值。
我尝试使用样式并将选择器设置为背景,但没有任何效果,我想到了自定义微调器,但没有得到任何有用的东西。
更新
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/spinner_consigntype"
android:prompt="@string/spinner_consign" />
</LinearLayout>
Activity_Test.Java
package com.mkyong.android;
import android.app.Activity;
import android.os.Bundle;
public class Activity_Test extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity__test);
addListenerOnSpinnerItemSelection();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity__test, menu);
return true;
}
public void addListenerOnSpinnerItemSelection(){
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
}
听众
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
在我的情况下,我得到的输出就像第一张图片一样。但是使用下载的代码,它会将输出显示为第二张图片。
【问题讨论】:
-
在你的活动中你必须添加一些项目。(即如果你想看到一些价值)......然后在你的spiiner中实现监听器。
-
谢谢,现在我已经添加了监听器,对于我在 xml 本身中分配的值。现在单击该项目,它将所选值显示为吐司。但是 UI 视图仍然是 spinner 的第一张图片。
-
为了记录,你想要的是 Holo 主题,这是从 Android 3.0 (Honeycomb) 开始的默认主题。它与 ASP.Net 无关
-
我尝试过像这样设置主题 android:theme="@android:style/Theme.Holo"。但是在这个背景变成黑色之后,微调器仍然看起来像我发布的第一张图片。