如何设置的最大长度为一个微调的下拉列表?如何设置、长度为、列表、最大

2023-09-05 00:33:48 作者:走向未来

我有一个微调打开时,目前掩盖了微调下面的一些文字。我需要限制离心器的最大下拉长度,既可以通过java的$ C $的c或通过XML,以便它不会遮挡这个文本

I have a spinner which currently obscures some text below the spinner when opened. I need to limit the maximum drop-down length of the spinner, either through java code or through XML, so that it does not obscure this text.

目前的设计是左边的例子,而所需的设计是在右边。

The current design is the left example while the desired design is on the right.

我如何去限制多远微调下降到打开的时候?在present,它下降到充满屏幕的整个部分下方。

How do I go about limiting how far the spinner drops down to when opened? At present, it drops down to fill the entire portion of screen below it.

推荐答案

要实现这一目标的方法之一是使用 ActionBarSherlock 的IcsSpinner。我做了必要的修改,以限制微调的大小这似乎很好地工作。

One way to achieve this is to use ActionBarSherlock's IcsSpinner. I made the needed modifications to limit the size of the spinner and that seems to work nicely.

请以下修改IcsListPopupWindow。

Make the following modifications to IcsListPopupWindow.

添加一个实例变量:

private int mDropDownVerticalOffsetBottom;

添加一个方法来设置此变量:

Add a method to set this variable:

public void setVerticalOffsetBottom(int offsetBottom) {
    mDropDownVerticalOffsetBottom = offsetBottom;
}

修改调用getMaxAvailableHeight到(mDropDownVerticalOffsetBottom加入):

Alter the call to getMaxAvailableHeight to (mDropDownVerticalOffsetBottom was added):

final int maxHeight = /*mPopup.*/getMaxAvailableHeight(
        mDropDownAnchorView, mDropDownVerticalOffset, mDropDownVerticalOffsetBottom, ignoreBottomDecorations);

更改方法的签名,以包括变量:

Change the method's signature to include that variable:

private int getMaxAvailableHeight(View anchor, int yOffset, int yOffsetBottom, boolean ignoreBottomDecorations) {

和认为,计算至底部的距离时偏移:

And consider that offset when computing the distance to the bottom:

final int distanceToBottom = bottomEdge - (anchorPos[1] + anchor.getHeight()) - yOffset - yOffsetBottom;

现在修改IcsSpinner.java实现setter方法​​的偏差:

Now modify IcsSpinner.java to implement the setter method for the offset:

private DropdownPopup mPopup;   // <- needs to be a DropdownPopup instead of a SpinnerPopup

public void setVerticalOffsetBottom(int offsetBottom) {
    mPopup.setVerticalOffsetBottom(offsetBottom);
}

现在所有你需要做的是设置偏移正确的值。以下是我做到了(我测试了它和它的工作在两个测试设备):

Now "all" you need to do is to set the offset to the correct value. Here's how I did it (I tested it and it worked on two test devices):

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塞满了整个窗户排除所有的装饰元素。

The assumption is that root_layout fills the whole window excluding all decorating elements.

最后一步是创建微调本身:

The final step is to create the spinner itself:

    // 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配有一张,为什么不利用呢?这当然不是写你自己一个人的工作更少。如果你不使用这个库的动作条带了所有的资源文件,你不需要和使用Proguard的剥离了所有未使用的类。 你可以利用AppCompat可能达到相同的(在这里看到:https://github.com/android/platform_frameworks_support/tree/master/v7/appcompat/src/android/support).

Now that might look complicated but as someone else mentioned, you'll not be able to achieve this without your own spinner implementation and since ActionBarSherlock comes with one, why not use it? It's certainly less work than writing your own one. If you don't use the library for the ActionBar strip away all resource files you don't need and use Proguard to strip away all unused classes. You could probably achieve the same using AppCompat (see here: https://github.com/android/platform_frameworks_support/tree/master/v7/appcompat/src/android/support).