Android:防止不同的微调器高度(空和满)
我在布局上有一些微调器,并使用loader从后台的db加载它们的数据。问题在于,在加载数据之前,空微调器的高度较小。因此,布局跳跃 我怎样才能防止这种跳跃 编辑(添加微调器行布局) 微调器行:Android:防止不同的微调器高度(空和满),android,layout,spinner,adapter,loader,Android,Layout,Spinner,Adapter,Loader,我在布局上有一些微调器,并使用loader从后台的db加载它们的数据。问题在于,在加载数据之前,空微调器的高度较小。因此,布局跳跃 我怎样才能防止这种跳跃 编辑(添加微调器行布局) 微调器行: <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Text1"
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Text1"
style="@style/StyleSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
微调器下拉列表行:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/style_spinner_row_padding" >
<TextView
android:id="@+id/Text1"
style="@style/StyleSpinnerDropDownRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/Text2"
style="@style/StyleSpinnerDropDownRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/Text1" />
</RelativeLayout>
指定的样式:
<!-- // Spinner // -->
<style name="StyleSpinner" parent="AppTheme">
<item name="android:textStyle">bold</item>
<item name="android:gravity">start</item>
<item name="android:textSize">@dimen/default_text_size_normal</item>
<item name="android:padding">@dimen/style_spinner_row_padding</item>
</style>
<style name="StyleSpinnerDropDownRow1" parent="AppTheme">
<item name="android:textStyle">bold</item>
<item name="android:textSize">@dimen/default_text_size_normal</item>
</style>
<style name="StyleSpinnerDropDownRow2" parent="AppTheme">
<item name="android:textStyle">normal</item>
<item name="android:textSize">@dimen/default_text_size_small</item>
</style>
大胆的
开始
@尺寸/默认值\文本\大小\正常值
@尺寸/样式\u微调器\u行\u填充
大胆的
@尺寸/默认值\文本\大小\正常值
正常的
@尺寸/默认值\文本\大小\小
屏幕截图:
我刚刚遇到了同样的问题,并设法为自己解决了,下面是我所做的 我正在为每个项目布局在我的微调器适配器中为android.R.layout.simple_spinner_dropdown_item充气。当我看到这个布局时,我注意到它没有边距,只有一个来自android:attr/dropdownlistpreferItemHeight的设置高度 所以我想我应该通过编程获得这个列表首选项高度值,并将其设置为微调器的最小高度。使用的答案,我能够得到正确的值,并设置为我的微调器
android.util.TypedValue value = new android.util.TypedValue();
getActivity().getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, value, true);
android.util.DisplayMetrics metrics = new android.util.DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
float ret = value.getDimension(metrics);
mSpinner.setMinimumHeight((int) (ret - 1 * metrics.density));
我删除了1dip,因为返回值似乎已向上舍入,这使得它太高了1像素
现在,我的所有微调器都具有相同的高度,无论它们是否有项或为空。为了防止在后台加载时高度发生变化,我刚刚创建了一个无数组,并在xml中将条目设置为: 在array.xml中
<string-array name="none">
<item></item>
</string-array>
高度不会改变,因为一旦加载程序完成,它将替换微调器的内容。我使用了带有空数组的
阵列适配器
获得所需内容后,只需获得一个新适配器或使用相同的适配器并调用notifyDataSetChanged()
即可使用一个看起来像下拉列表的图像,并将其设置为每个微调器的背景。请让我知道这是否有效。如果可能的话,请通过链接并在那里放置一个空微调器。你可以看到那里没有发生高度波动。我使用的是微调器的标准方式(就像tuturial中的一样),但使用的是游标(数据库数据)。我为微调器和下拉菜单使用了两种自己的布局(我在描述中添加了XML)。非常感谢。这个周末我会试试。这个方法非常有效,为我自己的最小身高计算打下了基础。我降低了小屏幕尺寸的微调器高度<代码>float reduce=context.getResources().getDimension(R.dimen.style\u微调器\u reduced\u高度);float ret=value.getDimension(度量);spinner.setMinimumHeight((int)(ret-reduce-1*metrics.density))代码>reduce的默认值是12dp。我尝试过这个,但最小高度似乎比不应用此代码时的高度大得多,微调器中填充了真实的项目。对于任何想在xml上使用此选项的人,都可以像这样使用android:layout_height=“?attr/dropdownListPreferredItemHeight”我也会尝试这个解决方案。对我来说,这听起来是一个非常方便和简单的解决办法。没有用。此外,在微调器更新为真实项目之前单击时,它会显示一个空项目。
android:entries:@array/none