自定义背景微调 [英] Custom Spinner Background

查看:108
本文介绍了自定义背景微调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个微调选择颜色。颜色应在微调头显示。下面是截图:

I try to create a Spinner for choosing a color. The color should display in the Spinner-header. Here is a screenshot:

但是,正如你所看到的,颜色不填我的9补丁微调的背景(头)。这里是9补丁背景:

But as you can see, the color don't fill my 9-patch Spinner background (header). Here is the 9-patch background:

但我不明白为什么......结果
我实现了一个自定义的ArrayAdapter,这里是code:

But I don't understand why...
I implemented a custom ArrayAdapter, here is the code:

spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new MyArrayAdapter(this, R.layout.spinner_row, R.id.textView1, items);
spinner.setAdapter(adapter);

private class MyArrayAdapter extends ArrayAdapter<String>{

    public MyArrayAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
        super(context, resource, textViewResourceId, objects); 
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomViewSmall(position, convertView, parent);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.spinner_row, parent, false);
        View v = (View) row.findViewById(R.id.spinnerFrame);
        v.setBackgroundColor(getColor(position));
        return row;
    }

    public View getCustomViewSmall(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.spinner_row_small, parent, false);     
        row.setBackgroundColor(getColor(position));    
        return row;
    }

    private int getColor(int pos) {
        switch(pos) { 
        case 0:
            return colors[0]; // Blue
        case 1:
            return colors[1];  // Purple
        case 2:
            return colors[2];  // Orange
        case 3:
            return colors[3];  // Yellow
        case 4:
            return colors[4];  // Cyan
        }
        return colors[5];
    }
}

这是布局的code:结果
该spinner_row_small.xml(即在报头中的布局):

And here is the code of the layout:
The spinner_row_small.xml (that is the layout for the header):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerFrameSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</FrameLayout>

该spinner_row.xml(布局为行):

The spinner_row.xml (the layout for the rows):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="@color/white" >

<FrameLayout
    android:id="@+id/spinnerFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_marginBottom="1dp"
    android:layout_marginTop="1dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</FrameLayout>
</FrameLayout>

而main.xml中:

And the main.xml:

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/textview"
            android:spinnerMode="dropdown"      
            android:popupBackground="@drawable/spinner_background"        
            android:layout_marginBottom="30dp" />

我会很高兴,如果有人能帮助我。结果
祝商祺!

I would be very pleased if anybody can help my.
Best regards!

推荐答案

我看不出有什么错在你的code,但问题 9片图像是如此,不仅标志着空间将得到扩大,你只会得到图像的标记空间实际使用。这就是为什么你得到各方的空间(这是左无标记)。我认为,9片图像是没有太多复杂的色彩背景的简单实用。

I don't see anything wrong in your code but problem with nine patch image is that, only marked space will get enlarged and you will only get the marked space of image in real use. That is why you are getting space on all sides(which is left unmarked). I think nine patch images are useful for simple backgrounds without much color complexity.

这篇关于自定义背景微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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