模仿ListView行中的溢出微调器

时间:2012-06-27 05:08:02

标签: android android-layout

我正试图在Spinner内的ListView的每一行上加ListFragment

我希望它看起来像商店中的垂直溢出图像,但我无法弄清楚如何显示可点击的垂直溢出图像以显示选项。

enter image description here

它总是看起来像下面。我想删除“选项”并改为使用溢出图像。

enter image description here

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

从其他帖子中找到相关的想法并将它们组合起来,谢谢Stack Overflow。

Android: How to set spinner selector to own image/icon?

Declaring a custom android UI element using XML

How to get width and height of the image?

我们的想法是,您创建一个宽度为Spinner的宽度ImageView。{1}}。单击图像时,会显示下拉列表。当Spinner处于屏幕边缘时,我还没有测试过它的行为,可能会造成麻烦。我还需要调整Spinner的位置,但现在这个有效。

我的计划是从Spinner中捕获选择,然后根据点击的内容打开一个对话框/意图。这是它的样子。 (ImageView很微弱,但现在对我来说它主要是一个安置者)

点击

enter image description here

点击

enter image description here

这是我使用的一般代码,因为这对其他人来说似乎是可取的。

<强>值/ attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="OverflowSpinner">
        <attr name="imageResource" format="string" />
        <attr name="spinnerTextResource" format="string" />
    </declare-styleable>
</resources>

<强>值/ strings.xml中

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="spinner_array">
        <item>Skip</item>
        <item>View log</item>
    </string-array>
</resources>

<强>布局/ row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:awesome="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!-- stuff -->
    <com.blah.package.OverflowSpinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        awesome:imageResource="@drawable/ic_menu_moreoverflow_normal_holo_light"
        awesome:spinnerTextResource="@array/spinner_array"
        /> 
</RelativeLayout>

<强> OverflowSpinner.java

public class OverflowSpinner extends RelativeLayout {
    int mImage;
    int mStrings;

    public OverflowSpinner(Context context) {
        super(context);
    }

    public OverflowSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
        setupDisplay(context);
    }

    public OverflowSpinner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
        setupDisplay(context);
    }

    private void init(AttributeSet attrs) { 
        TypedArray attribs = getContext().obtainStyledAttributes(attrs, R.styleable.OverflowSpinner);
        // get attributes
        mImage = attribs.getResourceId(R.styleable.OverflowSpinner_imageResource, -1);
        mStrings = attribs.getResourceId(R.styleable.OverflowSpinner_spinnerTextResource, -1);

        attribs.recycle();
    }

    private void setupDisplay(Context context) {
        BitmapDrawable bitmap = (BitmapDrawable)this.getResources().getDrawable(mImage);
        int height = bitmap.getBitmap().getHeight();

    // set size of Spinner to 0 x height so it's "hidden"
    // the height is used to help position the Spinner in a nicer spot 
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, mStrings, android.R.layout.simple_spinner_item);

        // setup spinner
        final Spinner spinner = new Spinner(context);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setLayoutParams(lp);
        this.addView(spinner);

        // set size of image to be normal
        lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(ALIGN_PARENT_BOTTOM);

        ImageButton option = new ImageButton(context);
        option.setBackgroundResource(android.R.color.transparent);
        option.setImageResource(mImage);
        option.setLayoutParams(lp);
        // when clicking the image button, trigger the spinner to show
        option.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                spinner.performClick();
            }
        });
        this.addView(option);
    }
}