jam*_*mes 17 height android spinner
我有一个微调器,当打开时,它会遮挡旋转器下面的一些文本.我需要通过java代码或XML来限制微调器的最大下拉长度,这样它就不会模糊这个文本.
当前设计是左侧示例,而所需设计位于右侧.
我如何限制旋转器在打开时下降到多远?目前,它下降到填满它下面的整个屏幕部分.
实现此目的的一种方法是使用ActionBarSherlock的IcsSpinner.我做了必要的修改以限制微调器的大小,这似乎很好地工作.
对IcsListPopupWindow进行以下修改.
添加实例变量:
private int mDropDownVerticalOffsetBottom;
Run Code Online (Sandbox Code Playgroud)
添加一个方法来设置此变量:
public void setVerticalOffsetBottom(int offsetBottom) {
mDropDownVerticalOffsetBottom = offsetBottom;
}
Run Code Online (Sandbox Code Playgroud)
将对getMaxAvailableHeight的调用更改为(添加了mDropDownVerticalOffsetBottom):
final int maxHeight = /*mPopup.*/getMaxAvailableHeight(
mDropDownAnchorView, mDropDownVerticalOffset, mDropDownVerticalOffsetBottom, ignoreBottomDecorations);
Run Code Online (Sandbox Code Playgroud)
更改方法的签名以包含该变量:
private int getMaxAvailableHeight(View anchor, int yOffset, int yOffsetBottom, boolean ignoreBottomDecorations) {
Run Code Online (Sandbox Code Playgroud)
在计算到底部的距离时考虑偏移量:
final int distanceToBottom = bottomEdge - (anchorPos[1] + anchor.getHeight()) - yOffset - yOffsetBottom;
Run Code Online (Sandbox Code Playgroud)
现在修改IcsSpinner.java以实现偏移的setter方法:
private DropdownPopup mPopup; // <- needs to be a DropdownPopup instead of a SpinnerPopup
public void setVerticalOffsetBottom(int offsetBottom) {
mPopup.setVerticalOffsetBottom(offsetBottom);
}
Run Code Online (Sandbox Code Playgroud)
现在,您需要做的就是将偏移量设置为正确的值.这是我如何做到的(我测试了它,它在两个测试设备上工作):
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
}
});
Run Code Online (Sandbox Code Playgroud)
假设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);
Run Code Online (Sandbox Code Playgroud)
现在这可能看起来很复杂,但正如其他人提到的那样,如果没有自己的微调器实现,你将无法实现这一点,而且由于ActionBarSherlock附带了一个,为什么不使用它呢?写作自己的工作肯定少了.如果你不使用ActionBar的库去除你不需要的所有资源文件,并使用Proguard去除所有未使用的类.您可以使用AppCompat实现相同的功能(请参阅此处:https://github.com/android/platform_frameworks_support/tree/master/v7/appcompat/src/android/support).
归档时间: |
|
查看次数: |
7422 次 |
最近记录: |