Android Make Spinner看起来像ImageView

时间:2014-04-18 08:25:05

标签: android android-layout imageview drawable android-drawable

我看到this post提供了一个解决方案,使Spinner看起来像EditText。

我想要的是旋转器看起来像ImageView(选定的图像)。通过这种方式,Spinner完全剥离了不必要的填充和右下角的三角形。 (所以在屏幕上它是一个图像,但是如果你点击它,它会像Spinner一样打开。)

PS:我已经有一个自定义的Spinner,其中包含Images作为项目,但我仍然有一些填充问题和我想删除的微调三角形。

我以为我会在上面的帖子中使用代码,然后只需将edit_text更改为image_view(或类似的东西),但事实证明R.drawable(reference here)不包含ImageViews。 / p>

有谁知道我应该为ImageView使用哪个R.drawable,或者我是否应该使用完全不同的方法将Spinner减少到所选的Image,那会是什么?

提前感谢您的回复。

1 个答案:

答案 0 :(得分: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();
    }
};

...