2016-02-26 117 views
0

我有一個漂亮的綠色背景,顯示在微調器後面 - 微調器位於背景前面。AppCompatSpinner:打開和關閉對話框時更改佈局

你可以看到,它看起來非常糟糕,因爲微調器可以清楚地區分它的背景。

我想使微調框背景透明,當微調對話框是已關閉。當對話框打開時,微調器可以呈現深綠色。目前,當對話框打開或關閉,同樣深綠色的佈局用於:

飛旋目前看在關閉時:

enter image description here

我想微調看起來像關閉時

enter image description here

什麼微調時像OPENED:

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> 

有沒有辦法加載lay當對話框關閉時,以及當對話框打開時,輸出xml以便我可以達到我想要的效果?

+0

您可以發佈你想要的封閉式和開放式的佈局。如果屏幕截圖未被剪切,這也可能有所幫助。 – Helix

+0

我已經發布了一個更新的屏幕截圖,用於顯示微調開啓時的外觀 - 您能看到我想要做什麼嗎?我只想爲旋轉工具關閉時的透明背景以及打開時的深綠色背景。 – Simon

+0

如果這就是所有你想改變它將更有效地調用'spinner.setBackgroundResource(R.drawable.your_background);' – Helix

回答

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; 
    } 
} 
相關問題