我遇到了ScrollView调整大小时非常烦人的问题,而且我将用尽可能的解决方案。ScrollView在儿童更改后不会直接调整大小
我有一个FragmentPager包含几个不同的片段,其中一个有一个ScrollView。 带有ScrollView的片段由一个Spinner和包含LinearLayout的ScrollView组成,其中包含几行其他视图(如SeekBars,Buttons,Edittexts)。 根据在Spinner中选择哪个选项,ScrollView显示不同的视图。要做到这一点,一些视图会将其可见性转换为“已过期”,而其他视图则转向“可见”。 这个效果很好,除了ScrollView在使用微调器选择不同的选项时似乎不能正确调整自身的大小。
当ScrollView充满视图并因此可滚动时,如果用户选择显示少于Viewsthan填充视口的选项,ScrollView仍会滚动。 当用户再次选择旧选项时,ScrollView现在无法滚动,因为它占用了上一个选项所需的大小。 当用户再次选择SAME选项时,ScrollView会突然滚动,因为它现在被调整为所需的实际大小。
这是怎么回事?更好的是,我该如何解决这个烦人的问题?
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/control_scroll"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#99000000"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DD000000"
android:gravity="bottom"
android:orientation="vertical" >
<TextView
android:id="@+id/lamp_choose_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:paddingLeft="5dp"
android:text="@string/choose_lamp_text"
android:textColor="#FFFFFF"
android:textSize="14sp" />
</LinearLayout>
<Spinner
android:id="@+id/lamp_select_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="#FFFFFF" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DD000000"
android:gravity="bottom"
android:orientation="vertical" >
<TextView
android:id="@+id/lamp_settings_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:background="#44000000"
android:paddingLeft="5dp"
android:text="@string/lamp_settings_text"
android:textColor="#FFFFFF"
android:textSize="14sp" />
</LinearLayout>
<!-- Lamp name -->
<LinearLayout
android:id="@+id/naam_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:paddingBottom="3dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="3dp" >
<TextView
android:id="@+id/bridge_naam"
style="@style/ConfigText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/config_lightname"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<EditText
android:id="@+id/lamp_naam_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@drawable/tasstextfield"
android:inputType="textNoSuggestions"
android:textColor="#FFFFFF" >
</EditText>
</LinearLayout>
<View
android:id="@+id/separator"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:background="@android:color/black"
android:visibility="visible" />
<!-- Rest of the views -->
事情我已经尝试过:
- ForceLayout/requestLayout父滚动型
- 无效滚动型
- ForceLayout/requestLayout上含有的LinearLayout
- 使LinearLayout无效
- 无效的滚动型
- ForceLayout/requestLayout的所有儿童上的滚动型
您是否试过使用['ListView'](http://developer.android.com/guide/topics/ui/layout/listview.html)而不是构建似乎是您自己的列表视图?忽略该页面上的'Loader'示例 - 您可以简单地使用每段文本创建一个String []或List []并将其提供给['ArrayAdapter'](http://developer.android)。 com/reference/android/widget/ArrayAdapter.html),然后重写'getView'来控制外观。 –
user113215
2013-03-26 12:41:26
我明白你为什么这么想。实际上,我停止使用TextViews,因为它基本上只是用于测试目的,现在使用不同的视图,如Buttons,EditTexts和Seekbars。但问题仍然存在。 – 2013-03-26 14:39:03
我相信问题是'ScrollView'需要调整自己的新'Views'。当你设置内部视图设置为'View.VISIBLE'时,在父ScrollView上调用'requestLayout()'或'forceLayout()'来安排布局传递。 – DeeV 2013-03-26 14:42:06