Android Spinner项目空间

时间:2014-08-05 05:40:23

标签: android xml spinner

我的应用中有两个微调器。一个微调器静态加载要显示的XML文件中的列表项。其他微调器从数据库中获取字符串列表并显示。我为两个微调器使用相同的XML属性。但两个项目之间的间距是不同的。具有静态字符串列表的微调器在项之间具有更多间距。从数据库加载项目的微调器具有某种包装高度。这些项目彼此接近,使用户难以选择。 解决这个问题的任何方法?

  <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Spinner
        android:id="@+id/spinner3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

java代码

static_sp = (Spinner) findViewById(R.id.spinner1);        
    List<String> array_karant = Arrays.asList(getResources().getStringArray(R.array.karant_list));
    ArrayAdapter<String> karant_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_karant);
    karant_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    static_sp.setAdapter(karant_adapter);
    static_sp.setSelection(0);
    static_sp.setOnItemSelectedListener(new select_karant());


    database_sp = (Spinner) findViewById(R.id.spinner3);  
    return_likes = db.getAllLikeList();
    ArrayAdapter<String> like_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, return_likes);
             like_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    database_sp.setAdapter(like_adapter);
    database_sp.setOnItemSelectedListener(new select_like());


图片链接在这里:

https://img.photobucket.com/albums/v311/flowofthoughts/SPINNER_zpsd5a86cf1.png

谢谢, 萨米尔

2 个答案:

答案 0 :(得分:0)

创建布局simple_spinner_item

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/text1"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

并更改您的代码

static_sp = (Spinner) findViewById(R.id.spinner1);        
List<String> array_karant = Arrays.asList(getResources().getStringArray(R.array.karant_list));
ArrayAdapter<String> karant_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_karant);
karant_adapter.setDropDownViewResource(R.layout.simple_spinner_item);
static_sp.setAdapter(karant_adapter);
static_sp.setSelection(0);
static_sp.setOnItemSelectedListener(new select_karant());


database_sp = (Spinner) findViewById(R.id.spinner3);  
return_likes = db.getAllLikeList();
ArrayAdapter<String> like_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, return_likes);
         like_adapter.setDropDownViewResource(R.layout.simple_spinner_item);
database_sp.setAdapter(like_adapter);
database_sp.setOnItemSelectedListener(new select_like());

如果你想要间距你必须将填充添加到textview

答案 1 :(得分:0)

创建一个布局simple_spinner_item.xml,就像这样

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:textAppearance="?android:attr/textAppearanceListItemSmall"
 android:gravity="center_vertical"
 android:paddingStart="?android:attr/listPreferredItemPaddingStart"
 android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
 android:background="?android:attr/activatedBackgroundIndicator"
 android:minHeight="?android:attr/listPreferredItemHeightSmall"
 android:layout_marginTop="0dp"
 android:layout_marginBottom="0dp"/>

最后两行是为了在每个项目之间添加空格,然后你修改了你的类,在这一行:

karant_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_karant);

因为您正在使用Android布局,我们需要使用我们的布局(删除单词android。):

karant_adapter = new ArrayAdapter<String>(this, R.layout.simple_spinner_item, array_karant);