gpt4 book ai didi

Android Spinner 尺寸非常大

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:34 25 4
gpt4 key购买 nike

我正在尝试像在我的应用程序中那样获得一个 ICS 微调器,并且玩了几个小时,最后我使用 HoloEverywhere 来获得它,它正在工作,但我有一个小设计问题,微调器是没有像我在 xml 中设置的那样包装它的内容,默认情况下看起来像这样:

enter image description here

真的,我在谷歌上搜索了几个小时,我发现的只是如何调整微调器项目的大小而不是 View 本身,这意味着我希望微调器像这样调整到所选项目的大小:

这是我的 XML:

<RelativeLayout 
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal" >

<org.holoeverywhere.widget.Spinner
android:id="@+id/spnCities"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
/>

<TextView
android:id="@+id/tvCities"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="@string/city"
android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

最佳答案

Spinner 的项目有最大宽度,但最多为父级宽度(在 layout_width = wrap_content 上)。您可以在 org.holoeverywhere.widget 包中创建 CustomSpinner 类并覆盖方法 measureContentWidth:

@Override
int measureContentWidth(SpinnerAdapter adapter, Drawable background) {
if (adapter == null) {
return 0;
}
View view = adapter.getView(getSelectedItemPosition(), null, this);
if (view.getLayoutParams() == null) {
view.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
view.measure(MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));
int width = view.getMeasuredWidth();
if (background != null) {
Rect mTempRect = new Rect();
background.getPadding(mTempRect);
width += mTempRect.left + mTempRect.right;
}
return width;
}

关于Android Spinner 尺寸非常大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13954917/

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