创建一个看起来像ASP.Net下拉列表的微调器

时间:2013-06-11 10:35:02

标签: android android-spinner

我需要创建一个只有三个值的下拉列表。我用旋转器完成了这个。但它看起来像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

}

}

在我的情况下,我得到的输出就像第一张图像一样。但是使用下载的代码,它将输出显示为第二个图像。

1 个答案:

答案 0 :(得分:1)

制作一个selector_spinner.xml:

item android:state_pressed =“true”android:drawable =“@ drawable / spinner_normal”
 item android:state_focused =“true”android:drawable =“@ drawable / spinner_normal”
 item 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);
相关问题