【问题标题】:Mimic the Overflow Spinner in a ListView Row模仿 ListView 行中的溢出微调器
【发布时间】:2012-06-27 05:08:02
【问题描述】:

我正在尝试在ListFragment 内的ListView 的每一行上放置一个Spinner

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

它总是如下所示。我想删除“选项”并改为使用溢出图像。

感谢任何帮助。

【问题讨论】:

  • 感谢您的回复。但是,这描述了向Spinner 添加项目时遇到问题,这不是我的麻烦。我正在寻找模仿溢出微调器

标签: android android-layout


【解决方案1】:

从其他帖子中找到相关的想法并结合起来,谢谢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?

这个想法是您创建一个 0dp 宽度 Spinner 并在其上加上 ImageView。当您单击图像时,它会显示下拉菜单。当 Spinner 位于屏幕边缘时,我还没有测试它的行为,并且很可能会引起麻烦。我还需要调整 Spinner 的位置,但目前可行。

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

点击前

点击后

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

values/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>

values/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>

layouts/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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    相关资源
    最近更新 更多