如何更改打开的微调器的位置?

发布于 2024-11-05 13:25:37 字数 126 浏览 3 评论 0原文

我希望微调器下拉菜单在微调器本身下方打开。 例如:

在此处输入图像描述

如何设置微调器下拉菜单的位置?

I would like the spinner dropdown to open right below the spinner itself.
E.g.:

enter image description here

How can i set the position of spinner dropdown?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花伊自在美 2024-11-12 13:25:37

将微调器模式声明为下拉菜单:

android:spinnerMode="dropdown"

然后使用垂直偏移来缩小间隙:

android:dropDownVerticalOffset="-15dp"

Spinner Android 文档

Declare the spinner mode as a dropdown:

android:spinnerMode="dropdown"

then use the vertical offset to close the gap:

android:dropDownVerticalOffset="-15dp"

Spinner Android Documentation

暮色兮凉城 2024-11-12 13:25:37

您必须扩展 Spinner 并更改 AlertDialog 的位置(单击时的 Spinner 充当警报对话框)。

代码(不仅仅是位置,它还为打开的微调器设置背景):

public class CustomSpinner extends Spinner {

private AlertDialog mPopup;

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

public CustomSpinner(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomSpinner(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
}

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();

    if (mPopup != null && mPopup.isShowing()) {
        mPopup.dismiss();
        mPopup = null;
    }
}


//when clicked alertDialog is made
@Override
public boolean performClick() {
    Context context = getContext();

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    CharSequence prompt = getPrompt();
    if (prompt != null) {
        builder.setTitle(prompt);
    }



    mPopup = builder.setSingleChoiceItems(
            new DropDownAdapter(getAdapter()),
            getSelectedItemPosition(), this).show();

    WindowManager.LayoutParams WMLP = mPopup.getWindow().getAttributes();

            //width and height must be set to anything other than WRAP_CONTENT!
    WMLP.x = 0; // x position
    WMLP.y = 50; // y position
    WMLP.height =390 ; //LayoutParams.WRAP_CONTEN
    WMLP.width = 315;
    WMLP.horizontalMargin = 0; 
    WMLP.verticalMargin = 0;

    mPopup.getWindow().setAttributes(WMLP);



    //ListView.getDefaultSize(size, measureSpec)
    ListView listView = mPopup.getListView();
    //listView.set
    // Remove divider between rows
    listView.setDivider(null);

    // Set custom background
    listView.setBackgroundResource(R.drawable.drop);

    // Remove background from all (grand)parent's
    ViewParent parent = listView.getParent();
    while (parent != null && parent instanceof View) {
        ((View) parent).setBackgroundDrawable(null);

        parent = parent.getParent();
    }

    return true;
}

@Override
public void onClick(DialogInterface dialog, int which) {
    setSelection(which);
    dialog.dismiss();
    mPopup = null;
}


 * <p>Wrapper class for an Adapter. Transforms the embedded Adapter instance
 * into a ListAdapter.</p>
 */
private static class DropDownAdapter implements ListAdapter, SpinnerAdapter {
    private SpinnerAdapter mAdapter;

    /**
     * <p>Creates a new ListAddapter wrapper for the specified adapter.</p>
     *
     * @param adapter the Adapter to transform into a ListAdapter
     */
    public DropDownAdapter(SpinnerAdapter adapter) {
        this.mAdapter = adapter;
    }

    public int getCount() {
        return mAdapter == null ? 0 : mAdapter.getCount();
    }

    public Object getItem(int position) {
        return mAdapter == null ? null : mAdapter.getItem(position);
    }

    public long getItemId(int position) {
        return mAdapter == null ? -1 : mAdapter.getItemId(position);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        return getDropDownView(position, convertView, parent);
    }

    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return mAdapter == null ? null :
                mAdapter.getDropDownView(position, convertView, parent);
    }

    public boolean hasStableIds() {
        return mAdapter != null && mAdapter.hasStableIds();
    }

    public void registerDataSetObserver(DataSetObserver observer) {
        if (mAdapter != null) {
            mAdapter.registerDataSetObserver(observer);
        }
    }

    public void unregisterDataSetObserver(DataSetObserver observer) {
        if (mAdapter != null) {
            mAdapter.unregisterDataSetObserver(observer);
        }
    }

    /**
     * <p>Always returns false.</p>
     *
     * @return false
     */
    public boolean areAllItemsEnabled() {
        return true;
    }

    /**
     * <p>Always returns false.</p>
     *
     * @return false
     */
    public boolean isEnabled(int position) {
        return true;
    }

    public int getItemViewType(int position) {
        return 0;
    }

    public int getViewTypeCount() {
        return 1;
    }

    public boolean isEmpty() {
        return getCount() == 0;
    }

   }
   }

然后您只需使用“yourPackage.CustomSpinner”元素将其插入到您的布局中,例如:

<yourPackage.CustomSpinner 
     android:layout_height="wrap_content"
     android:id="@+id/spinner" 
     android:layout_width="fill_parent">
</yourPackage.CustomSpinner>

You have to extend Spinner and change the location of AlertDialog (spinner when clicked acts as alertDialog).

Code (does a bit more than just position, it also sets background for opened spinner):

public class CustomSpinner extends Spinner {

private AlertDialog mPopup;

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

public CustomSpinner(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomSpinner(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
}

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();

    if (mPopup != null && mPopup.isShowing()) {
        mPopup.dismiss();
        mPopup = null;
    }
}


//when clicked alertDialog is made
@Override
public boolean performClick() {
    Context context = getContext();

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    CharSequence prompt = getPrompt();
    if (prompt != null) {
        builder.setTitle(prompt);
    }



    mPopup = builder.setSingleChoiceItems(
            new DropDownAdapter(getAdapter()),
            getSelectedItemPosition(), this).show();

    WindowManager.LayoutParams WMLP = mPopup.getWindow().getAttributes();

            //width and height must be set to anything other than WRAP_CONTENT!
    WMLP.x = 0; // x position
    WMLP.y = 50; // y position
    WMLP.height =390 ; //LayoutParams.WRAP_CONTEN
    WMLP.width = 315;
    WMLP.horizontalMargin = 0; 
    WMLP.verticalMargin = 0;

    mPopup.getWindow().setAttributes(WMLP);



    //ListView.getDefaultSize(size, measureSpec)
    ListView listView = mPopup.getListView();
    //listView.set
    // Remove divider between rows
    listView.setDivider(null);

    // Set custom background
    listView.setBackgroundResource(R.drawable.drop);

    // Remove background from all (grand)parent's
    ViewParent parent = listView.getParent();
    while (parent != null && parent instanceof View) {
        ((View) parent).setBackgroundDrawable(null);

        parent = parent.getParent();
    }

    return true;
}

@Override
public void onClick(DialogInterface dialog, int which) {
    setSelection(which);
    dialog.dismiss();
    mPopup = null;
}


 * <p>Wrapper class for an Adapter. Transforms the embedded Adapter instance
 * into a ListAdapter.</p>
 */
private static class DropDownAdapter implements ListAdapter, SpinnerAdapter {
    private SpinnerAdapter mAdapter;

    /**
     * <p>Creates a new ListAddapter wrapper for the specified adapter.</p>
     *
     * @param adapter the Adapter to transform into a ListAdapter
     */
    public DropDownAdapter(SpinnerAdapter adapter) {
        this.mAdapter = adapter;
    }

    public int getCount() {
        return mAdapter == null ? 0 : mAdapter.getCount();
    }

    public Object getItem(int position) {
        return mAdapter == null ? null : mAdapter.getItem(position);
    }

    public long getItemId(int position) {
        return mAdapter == null ? -1 : mAdapter.getItemId(position);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        return getDropDownView(position, convertView, parent);
    }

    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return mAdapter == null ? null :
                mAdapter.getDropDownView(position, convertView, parent);
    }

    public boolean hasStableIds() {
        return mAdapter != null && mAdapter.hasStableIds();
    }

    public void registerDataSetObserver(DataSetObserver observer) {
        if (mAdapter != null) {
            mAdapter.registerDataSetObserver(observer);
        }
    }

    public void unregisterDataSetObserver(DataSetObserver observer) {
        if (mAdapter != null) {
            mAdapter.unregisterDataSetObserver(observer);
        }
    }

    /**
     * <p>Always returns false.</p>
     *
     * @return false
     */
    public boolean areAllItemsEnabled() {
        return true;
    }

    /**
     * <p>Always returns false.</p>
     *
     * @return false
     */
    public boolean isEnabled(int position) {
        return true;
    }

    public int getItemViewType(int position) {
        return 0;
    }

    public int getViewTypeCount() {
        return 1;
    }

    public boolean isEmpty() {
        return getCount() == 0;
    }

   }
   }

Then you just gotta insert it to your layout with "yourPackage.CustomSpinner" element like:

<yourPackage.CustomSpinner 
     android:layout_height="wrap_content"
     android:id="@+id/spinner" 
     android:layout_width="fill_parent">
</yourPackage.CustomSpinner>
坐在坟头思考人生 2024-11-12 13:25:37

对于下拉微调器模式,您可以使用此:

mSortingSpinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        mSortingSpinner.setDropDownVerticalOffset(
                mSortingSpinner.getDropDownVerticalOffset() + mSortingSpinner.getHeight());
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            mSortingSpinner.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        } else {
            mSortingSpinner.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    }
});

这按微调器高度设置下拉列表的垂直偏移。

For dropdown Spinner mode you can use this:

mSortingSpinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        mSortingSpinner.setDropDownVerticalOffset(
                mSortingSpinner.getDropDownVerticalOffset() + mSortingSpinner.getHeight());
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            mSortingSpinner.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        } else {
            mSortingSpinner.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    }
});

This set vertical offset of dropdown by spinner height.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文