【发布时间】:2014-02-20 23:00:02
【问题描述】:
我有一个微调器,当前它在打开时会遮挡微调器下方的一些文本。我需要通过 java 代码或通过 XML 限制微调器的最大下拉长度,以便它不会模糊此文本。
当前设计是左侧示例,而所需设计在右侧。
如何限制旋转器在打开时下降的距离?目前,它会下降以填满其下方的整个屏幕部分。
【问题讨论】:
-
那是另一种情况 keshav;我问的是限制最大高度,但该问题涉及微调器中文本元素的最大宽度。
我有一个微调器,当前它在打开时会遮挡微调器下方的一些文本。我需要通过 java 代码或通过 XML 限制微调器的最大下拉长度,以便它不会模糊此文本。
当前设计是左侧示例,而所需设计在右侧。
如何限制旋转器在打开时下降的距离?目前,它会下降以填满其下方的整个屏幕部分。
【问题讨论】:
实现此目的的一种方法是使用ActionBarSherlock 的 IcsSpinner。我进行了必要的修改以限制微调器的大小,这似乎工作得很好。
对 IcsListPopupWindow 进行以下修改。
添加实例变量:
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)。
【讨论】:
我已经搜索了很多,似乎在任何地方都找不到解决方案。通过重新创建微调器的功能,您可能不得不走一条稍微不同的路线,但要使用按钮和对话框。
假设您使用 ArrayAdapter 作为微调器,试试这个:
将 Spinner 替换为带有向下箭头图标的按钮。
创建自定义Dialog 或AlertDialog 并使用ArrayAdapter 填充ListView。
对于onClick 方法,按下所选选项的值并使用所选值填充static 变量和按钮文本。这样,您可以将对话框的大小设置为任何您想要的大小,并且多余的选项将始终可以滚动。
我知道这并不理想,但我还没有找到任何方法来更改微调器的“下拉”部分的高度。
很抱歉,如果这不是您所希望的。
编辑:以前有人在这里问过完全相同的问题,结果他们采用了我刚才描述的确切方法。您可以在此处查看他们使用的示例代码:Check out the question/code
【讨论】:
你可以使用反思。
Spinner spinner = (Spinner) findViewById(R.id.spinner);
try {
Field popup = Spinner.class.getDeclaredField("mPopup");
popup.setAccessible(true);
// Get private mPopup member variable and try cast to ListPopupWindow
android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner);
// Set popupWindow height to 500px
popupWindow.setHeight(500);
}
catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
// silently fail...
}
但是如果你使用的是 androidx.appCompat 1.1.0 那么你必须使用 ->
I can't use API reflection on androidx.appcompat:appcompat:1.1.0
问候
【讨论】:
我认为您必须使用自定义微调器来满足您的要求。没有任何内置方法或 xml 属性可以提供帮助。
请参阅此示例以获取帮助。
https://github.com/ankitthakkar/DropDownSpinner
希望这会奏效。
注意:这是更改后的答案。
【讨论】: