android - 如何设置微调器下拉列表的最大长度?

标签 android height spinner

我有一个微调器,当前它在打开时会遮挡微调器下方的一些文本。我需要通过 java 代码或通过 XML 限制微调器的最大下拉长度,以使其不会模糊此文本。

当前设计是左侧示例,而所需设计在右侧。 explanation

如何限制旋转器在打开时下降的距离?目前,它会下降以填满其下方的整个屏幕部分。

最佳答案

实现此目的的一种方法是使用 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)。

关于android - 如何设置微调器下拉列表的最大长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21426038/

相关文章:

Android、REST 和 HATEOAS 约束

android - Android/iPhone 图像识别

html - css 中间 div 滚动和 100% 高度

jQuery - 动态 div 高度等于整个窗口的高度

Android - 显示/隐藏 fragment 留下空白区域

css - 如何使字体全高

android - 如何阻止旋转器自动选择选项?

添加适配器后,Android Spinner 保持为空

android - 当警报对话框打开并且手机(模拟器)改变其方向时,应用程序崩溃

java - Android、Retrofit、RxJava - 将多个实体的字段合并为一个实体