为了对齐spinner 真的是难,网上找了好多,基本无解,有的说是theme 的问题,试过之后发现使用推荐的theme 也没什么用,其他的基本是扯淡。
只能去看一下源码
/**
* Set a horizontal offset in pixels for the spinner's popup window of choices.
* Only valid in {@link #MODE_DROPDOWN}; this method is a no-op in other modes.
*
* @param pixels Horizontal offset in pixels
*
* @attr ref android.R.styleable#ListPopupWindow_dropDownHorizontalOffset
*/
public void setDropDownHorizontalOffset(int pixels) {
mPopup.setHorizontalOffset(pixels);
}
看到这里就知道这个方法肯定有用,于是就试了一下,果然可以。
源码分析:直接看show 方法,看起来dropdown 就是用的listview 实现的,这个与offset 无关不用管
public void show(int textDirection, int textAlignment) {
final boolean wasShowing = isShowing();
computeContentWidth();
setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
super.show();
final ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setTextDirection(textDirection);
listView.setTextAlignment(textAlignment);
setSelection(Spinner.this.getSelectedItemPosition());
}
void computeContentWidth() {
if (isLayoutRtl()) {
hOffset += spinnerWidth - spinnerPaddingRight - getWidth();
} else {
hOffset += spinnerPaddingLeft;
}
setHorizontalOffset(hOffset);
}
但是这个show 方法每次都会调用的,每次都会去计算。算了,断点调试不了,下次再试。
顺便给大家提个醒,自从跟风用了android 28 编译之后,调试源码的时候简直想死,因为手机 版本24,编译版本28 源码根本调试不了,建议用测试机相同的版本不要跟风。

本文介绍了一种解决Spinner在Android应用中对齐问题的方法。通过分析源码并利用`setDropDownHorizontalOffset`方法,实现了Spinner下拉菜单的精确位置调整。


被折叠的 条评论
为什么被折叠?



