如何在Android中更改微调器的布局 [英] How to change layout of spinner in Android

查看:50
本文介绍了如何在Android中更改微调器的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当O单击此微调器时,它会显示一个很大的下拉列表:

我想要一个很小的视图,如第二幅图所示.就像ASP.NET中的下拉菜单一样.像这样,宽度略有减小.

我使用了以下代码.任何帮助更改第二张图像的视图的方法,将不胜感激.

  Spinner spinner =(Spinner)findViewById(R.id.spinner);ArrayAdapter< CharSequence>适配器= ArrayAdapter.createFromResource(R.array.planets_array,android.R.layout.simple_spinner_item);adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);spinner.setAdapter(adapter); 

解决方案

这是一篇不错的文章: 的信息

并且可能有用:自定义纺手

When O click on this spinner it gives a big dropdown:

I want a very small view as in the second image. Just like dropdowns in ASP.NET. Like this with width little more reduced.

I used the following code. Any help to change the view of second image will be appreciated.

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
       this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

解决方案

This is a good article : Customizing the Action Bar

And also you can try this :

Design you own customized drawable for spinner background and apply to it. For spinnerbackground.xml images you can refer the images from the SDK. recreate the images as per your design requirements

"Android-sdk\platforms\android-9\data\res\drawable-hdpi\*.png"

spinnerbackground.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/wlbtn_dropdown_normal" />
    <item
        android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/wlbtn_dropdown_disabled" />
    <item
        android:state_pressed="true"
        android:drawable="@drawable/wlbtn_dropdown_pressed" />
    <item
        android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/wlbtn_dropdown_selected" />
    <item
        android:state_enabled="true"
        android:drawable="@drawable/wlbtn_dropdown_normal" />
    <item
        android:state_focused="true"
        android:drawable="@drawable/wlbtn_dropdown_disabled_focused" />
    <item
        android:drawable="@drawable/wlbtn_dropdown_disabled" />
</selector>

then for spinner widget apply your custom drawable:

<Spinner android:background="@drawable/spinnerbackground"
         android:id="@+id/spinnerIDr"
         android:layout_height="wrap_content" 
         android:layout_width="fill_parent">
    </Spinner>

Edited :

<Spinner android:background="@drawable/spinnerbackground"
         android:id="@+id/spinnerIDr"
         android:popupBackground="@drawable/popup_background"
         android:layout_height="wrap_content" 
         android:layout_width="fill_parent">
    </Spinner>

where popup_background is :

and

Design your custom layout for spinner texts as (name : custom_spiner.xml)

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="4dp"
    android:textSize="14sp"
    android:typeface="serif"
    android:singleLine="true"
    android:layout_marginLeft="2dip"
    android:layout_marginRight="5dip"
    android:ellipsize="marquee"
    android:textColor="#000000">
</TextView>

and use it as

adapter.setDropDownViewResource(R.layout.custom_spiner);

in your code.

Edited 2:

if you want to do this using java code read about PopupWindow

And may be useful : custom-spinner

这篇关于如何在Android中更改微调器的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆