实现此目的的一种方法是使用动作栏夏洛克 http://actionbarsherlock.com/的 IcsSpinner。我做了必要的修改来限制旋转器的大小,这似乎工作得很好。
对 Ics ListPopupWindow 进行以下修改。
添加实例变量:
private int mDropDownVerticalOffsetBottom;
添加一个方法来设置此变量:
public void setVerticalOffsetBottom(int offsetBottom) {
mDropDownVerticalOffsetBottom = offsetBottom;
}
将 getMaxAvailableHeight 的调用更改为(添加了 mDropDownVerticalOffsetBottom):
final int maxHeight = /*mPopup.*/getMaxAvailableHeight(
mDropDownAnchorView, mDropDownVerticalOffset, mDropDownVerticalOffsetBottom, ignoreBottomDecorations);
更改方法的签名以包含该变量:
private int getMaxAvailableHeight(View anchor, int yOffset, int yOffsetBottom, boolean ignoreBottomDecorations) {
计算到底部的距离时考虑该偏移:
final int distanceToBottom = bottomEdge - (anchorPos[1] + anchor.getHeight()) - yOffset - yOffsetBottom;
现在修改 IcsSpinner.java 来实现偏移量的 setter 方法:
private DropdownPopup mPopup; // <- needs to be a DropdownPopup instead of a SpinnerPopup
public void setVerticalOffsetBottom(int offsetBottom) {
mPopup.setVerticalOffsetBottom(offsetBottom);
}
现在“所有”您需要做的就是将偏移量设置为正确的值。我是这样做的(我测试了它并且它在两个测试设备上工作):
final View root = findViewById(R.id.root_layout);
final View view = findViewById(R.id.view_not_to_be_obscured);
root.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
root.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int[] locations = new int[2];
root.getLocationOnScreen(locations);
int y1 = locations[1];
int h1 = root.getHeight();
view.getLocationOnScreen(locations);
int y2 = locations[1];
int offset = y1 + h1 - y2;
// here we initialize the spinner
}
});
假设 root_layout 填充整个窗口,不包括所有装饰元素。
最后一步是创建微调器本身:
// actionDropDownStyle is an attribute set in the theme to e.g. @style/Widget.Sherlock.Spinner.DropDown.ActionBar or @style/Widget.Sherlock.Light.Spinner.DropDown.ActionBar for light theme
IcsSpinner spinner = new IcsSpinner(context, null, R.attr.actionDropDownStyle);
// yes here we set the offset!
spinner.setVerticalOffsetBottom(offset);
spinner.setPadding(spinner.getPaddingLeft(), 0, spinner.getPaddingRight(), 0);
spinner.setId(R.id.some_id);
spinner.setAdapter(yourAdapter); // you can use a simple ArrayAdapter or whatever you need
// create ICS LinearLayout
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
IcsLinearLayout linearLayout = (IcsLinearLayout) inflater.inflate(R.layout.abs__action_bar_tab_bar_view, null);
linearLayout .setPadding(listNavLayout.getPaddingLeft(), 0, listNavLayout.getPaddingRight(), 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER;
linearLayout .addView(spinner, params);
现在这可能看起来很复杂,但正如其他人提到的,如果没有自己的微调器实现,您将无法实现这一目标,并且既然 ActionBarSherlock 附带了一个,为什么不使用它呢?这肯定比自己编写一个工作量要少。如果您不使用 ActionBar 的库,请删除所有不需要的资源文件,并使用 Proguard 删除所有未使用的类。
您可能可以使用 AppCompat 实现相同的目标(请参见此处:https://github.com/android/platform_frameworks_support/tree/master/v7/appcompat/src/android/support https://github.com/android/platform_frameworks_support/tree/master/v7/appcompat/src/android/support).