【问题标题】:Create a spinner which look like the drop down of ASP.Net创建一个看起来像 ASP.Net 下拉菜单的微调器
【发布时间】: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"。但是在这个背景变成黑色之后,微调器仍然看起来像我发布的第一张图片。

标签: android android-spinner


【解决方案1】:

制作一个 selector_spinner.xml :

项目 android:state_pressed="true" android:drawable="@drawable/spinner_normal"
项目 android:state_focused="true" android:drawable="@drawable/spinner_normal"
项目 android:drawable="@drawable/spinner_normal"

在你的布局中使用这个选择器:main.xml

          <Spinner
            android:id="@+id/spinner_selection"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
           **android:background="@drawable/selector_spinner"**
            android:entries="@array/contract_rate_type" />

然后在类文件中,

   Spinner sp = (Spinner).findViewById(R.id.spinner_selection);

【讨论】:

  • 谢谢,我已经为按下和聚焦添加了 9patch 图像,看起来像下拉菜单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多