2014-04-18 52 views
0

我見過this post,有一個使Spinner看起來像EditText的解決方案。Android Make Spinner看起來像ImageView

我想要的是微調看起來像一個ImageView(選定的圖像)。通過這種方式,微調器完全沒有不必要的填充和右下角的三角形。 (所以在屏幕上它是一個圖像,但如果你點擊它,它就像一個微調打開。)

PS:我已經有一個自定義的圖像作爲項目的微調,但我仍然有一些填充問題和我想刪除的旋轉三角形。

我想我會使用上面的帖子中的代碼,然後簡單地將edit_text更改爲image_view(或類似的東西),但事實證明,R.drawable(reference here)不包括ImageViews。

有誰知道哪個Rdrawable我應該用於ImageView,或者如果我應該使用一個完全不同的方法來將Spinner減少到選定的圖像,那會是什麼?

在此先感謝您的回覆。

+2

爲什麼不在點擊imageview時在Fragment彈出框中生成列表視圖? – Skynet

+0

@ Nun'ehai嗯..這確實是一個好主意。創建彈出窗口非常簡單。將嘗試,謝謝! –

+0

歡迎,請不要猶豫,詢問您是否需要彈出窗口的更多幫助。 – Skynet

回答

2

在Nun'e Chai的評論之後,我做了一個PopupWindow,它觸發了一個ImageButton的點擊。所以謝謝你,Nun'e Chai。對於那些有興趣,下面是代碼:

在acitivity_main.xml:

<ImageButton 
    android:id="@+id/ibtnSpinner" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="15dp" 
    android:contentDescription="@string/checkbox_content_description" 
    android:src="@drawable/checkbox_unchecked" 
    android:background="@drawable/transparent_background" /> 

transparent_background.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" android:drawable="@android:color/transparent" /> 
    <item android:state_pressed="true" android:drawable="@android:color/transparent" /> 
    <item android:drawable="@android:color/transparent" /> 
</selector> 

spinner_popup.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/spinnerLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <ImageButton 
     android:id="@+id/simgUnchecked" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/checkbox_unchecked" 
     android:contentDescription="@string/checkbox_content_description" 
     android:background="@drawable/transparent_background" /> 

    <ImageButton 
     android:id="@+id/simgChecked" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/checkbox_checked" 
     android:contentDescription="@string/checkbox_content_description" 
     android:background="@drawable/transparent_background" /> 

    <ImageButton 
     android:id="@+id/simgError" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/checkbox_error" 
     android:contentDescription="@string/checkbox_content_description" 
     android:background="@drawable/transparent_background" /> 

    <ImageButton 
     android:id="@+id/simgPartly" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/checkbox_partly" 
     android:contentDescription="@string/checkbox_content_description" 
     android:background="@drawable/transparent_background" /> 

</LinearLayout> 

在MainActivity。 java:

private Point p; 
private ImageButton spinnerButton; 
private PopupWindow spinner; 

protected void onCreate(Bundle savedInstanceState) { 
    ... 

    addListenerToSpinnerButton(); 
} 

private void addListenerToSpinnerButton(){ 
    spinnerButton = (ImageButton) findViewById(R.id.ibtnSpinner); 
    spinnerButton.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      if(p != null) 
       showSpinner(MainActivity.this, p); 
     } 
    }); 
} 

// Get the x and y position after the button is drawn on screen 
// (It's important to note that we can't get the position in the onCreate(), 
// because at that stage most probably the view isn't drawn yet, so it will return (0, 0)) 
@Override 
public void onWindowFocusChanged(boolean hasFocus){ 
    int[] location = new int[2]; 
    ImageButton btn = (ImageButton) findViewById(R.id.ibtnPopup); 

    // Get the x, y location and store it in the location[] array 
    btn.getLocationOnScreen(location); 

    // Initialize the Point with x, and y positions 
    p = new Point(); 
    p.x = location[0]; 
    p.y = location[1]; 
} 

// The method that displays the spinner 
private void showSpinner(final ActionBarActivity context, Point p){ 
    // Inflate the spinner.xml 
    LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.spinnerLayout); 
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View layout = layoutInflater.inflate(R.layout.spinner, viewGroup); 

    // Creating the PopupWindow 
    spinner = new PopupWindow(context); 
    spinner.setContentView(layout); 
    spinner.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    spinner.setFocusable(true); 

    // Clear the default translucent background 
    // TODO: Fix deprecated to BitmapDrawable(Resource, Bitmap) 
    spinner.setBackgroundDrawable(new BitmapDrawable()); 

    // Displaying the spinner at the specified location, + offsets 
    spinner.showAtLocation(layout, Gravity.NO_GRAVITY, p.x, p.y); 

    // Getting a reference to the ImageButtons, and close the spinner when clicked 
    ImageButton optionUnchecked = (ImageButton) layout.findViewById(R.id.simgUnchecked); 
    optionUnchecked.setOnClickListener(spinnerOnClickListener); 
    optionUnchecked.setTag(R.drawable.checkbox_unchecked); 

    ImageButton optionChecked = (ImageButton) layout.findViewById(R.id.simgChecked); 
    optionChecked.setOnClickListener(spinnerOnClickListener); 
    optionChecked.setTag(R.drawable.checkbox_checked); 

    ImageButton optionError = (ImageButton) layout.findViewById(R.id.simgError); 
    optionError.setOnClickListener(spinnerOnClickListener); 
    optionError.setTag(R.drawable.checkbox_error); 

    ImageButton optionPartly = (ImageButton) layout.findViewById(R.id.simgPartly); 
    optionPartly.setOnClickListener(spinnerOnClickListener); 
    optionPartly.setTag(R.drawable.checkbox_partly); 
} 
private OnClickListener spinnerOnClickListener = new OnClickListener(){ 
    @Override 
    public void onClick(View v){ 
     // Get the id of the ImageButton that is clicked 
     ImageButton btn = (ImageButton) v; 
     int id = (Integer) btn.getTag(); // We are sure it's an Integer, so the cast from Object to int is safe 

     // Change the ImageButton that triggered the spinner to the same Image 
     spinnerButton.setImageResource(id); 

     // Close the spinner 
     if(spinner != null) 
      spinner.dismiss(); 
    } 
}; 

... 
+0

很高興你能工作:) – Skynet