我在布局上有一些微调器,并使用加载器在后台从数据库加载它们的数据。问题是在加载数据之前,空微调器的高度较小。所以布局跳跃。
如何防止这种跳跃?
编辑(添加微调器行布局)
微调器行:
<?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>
屏幕截图:
最佳答案
我刚刚遇到了同样的问题并设法自己修复了它,所以这就是我所做的。
我在微调器适配器中为每个项目布局充气 android.R.layout.simple_spinner_dropdown_item。当我查看此布局时,我注意到它没有边距,只有一个来自 android:attr/dropdownListPreferredItemHeight 的设定高度。
所以我想我应该以编程方式获取此列表的首选项目高度值,并将其设置为微调器的最小高度。使用 this stackoverflow question 的答案我能够获得正确的值并将其设置到我的微调器。
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 个像素。
现在我的所有微调器都具有相同的高度,无论它们是否有项目或为空。
关于Android:防止不同的微调器高度(空的和填充的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21513366/