AppCompatSpinner:打开和关闭对话框时更改布局

时间:2016-02-26 18:23:04

标签: android android-layout android-spinner appcompat-v7-r23

我有一个很好的绿色背景,我在旋转器后面显示 - 旋转器位于背景前面。

你可以看到它看起来很糟糕,因为微调器可以清楚地区别于它的背景。

我想在微调器的对话框 CLOSED 时使微调器背景透明。当对话框 OPENED 时,微调器可以呈现较深的绿色。目前,当对话框 OPENED或CLOSED 时,布局中使用相同的深绿色:

关闭时当前的SPINNER:

enter image description here

在关闭时我希望旋转器看起来像什么

enter image description here

SPINNER在打开时看起来像什么:

enter image description here

我使用适配器加载微调器:

    ArrayAdapter<String> ageAdapter = new ArrayAdapter<String>(this,
            R.layout.spinner_age, R.id.spinner_item, generateAgeRange());
    ageSpinner.setAdapter(ageAdapter);

我知道R.layout.spinner_age xml决定了微调器布局的外观:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:background="@color/primary_darker"
        android:textColor="@color/white"
        tools:text="Hello"
        android:gravity="left"
        android:padding="12dp"
        android:id="@+id/spinner_item"/>

</LinearLayout>

当对话框处于CLOSED状态时以及对话框打开时是否可以加载布局xml以便我可以达到我想要的效果?

1 个答案:

答案 0 :(得分:0)

已解决 - 您可以使用以下适配器来达到预期效果:

package com.example.simon.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.simon.activities.R;

import java.util.List;


public class GenderSpinAdapter extends ArrayAdapter<String> {

    private Context context1;
    private String[] data;
    LayoutInflater inflater;


    public GenderSpinAdapter(Context context, int resource) {
        super(context, resource);
    }

    public GenderSpinAdapter(Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
    }

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

    public GenderSpinAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
        super(context, resource, textViewResourceId, objects);
        context1 = context;
        data = objects;

        inflater = (LayoutInflater) context1
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public GenderSpinAdapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
    }

    public GenderSpinAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
        super(context, resource, textViewResourceId, objects);
    }

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

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

    // This funtion called for each row ( Called data.size() times )
    public View getCustomView(int position, View convertView, ViewGroup parent) {

        View row = inflater.inflate(R.layout.spinner_gender, parent, false);

        TextView tvCategory = (TextView) row.findViewById(R.id.spinner_item);

        tvCategory.setText(data[position].toString());

        return row;
    }

    // This funtion called for each row ( Called data.size() times )
    public View getNormalView(int position, View convertView, ViewGroup parent) {

        View row = inflater.inflate(R.layout.spinner_transparent, parent, false);

        TextView tvCategory = (TextView) row.findViewById(R.id.spinner_item);

        tvCategory.setText(data[position].toString());

        return row;
    }
}