gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:24:32 26 4
gpt4 key购买 nike

我有一个微调器,当前它在打开时会遮挡微调器下方的一些文本。我需要通过 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com