【发布时间】:2013-03-12 10:15:21
【问题描述】:
我遇到了一个非常烦人的关于调整 ScrollView 大小的问题,而且我已经没有可能的解决方案了。
我有一个 FragmentPager,其中包含几个不同的 Fragment,其中一个有一个 ScrollView。 带有 ScrollView 的 Fragment 由 Spinner 和包含 LinearLayout 的 ScrollView 组成,其中包含几行其他 View(例如 SeekBars、Buttons、Edittexts)。 根据在 Spinner 中选择的选项,ScrollView 显示不同的视图。为此,一些视图的可见性变为已消失,而另一些视图则变为可见。 这很好用,除了 ScrollView 在使用 Spinner 选择不同选项时似乎无法正确调整自身大小。
当 ScrollView 充满视图并因此可滚动时,如果用户选择的选项显示的 Views 少于填充 ViewPort 所需的 Views,则 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 -->
我已经尝试过的事情:
- 父 ScrollView 上的 ForceLayout/requestLayout
- 使 ScrollView 无效
- 包含 LinearLayout 的 ForceLayout/requestLayout
- 使线性布局无效
- 使 ScrollView 的所有子级无效
- ScrollView 的所有子项上的 ForceLayout/requestLayout
【问题讨论】:
-
您是否尝试过使用
ListView而不是构建您自己的列表视图?忽略该页面上的Loader示例——您可以简单地为每段文本创建一个String[]或List<String>,并将其提供给ArrayAdapter,然后覆盖getView以控制外观。 -
我明白你为什么会这么想。实际上,我停止使用 TextViews,因为那基本上只是为了测试目的,现在我使用不同的 Views,例如 Buttons、EditTexts 和 Seekbars。然而,问题仍然存在。
-
我认为问题在于
ScrollView需要为新的Views调整自身大小。当您将内部视图设置为View.VISIBLE时,在父 ScrollView 上调用requestLayout()或forceLayout()以安排布局传递。 -
用我已经尝试过的东西更新了我的问题,似乎他们第一次没有坚持。可悲的是,我已经尝试在所有视图上使 requestLayout 和 forceLayout 失效。问题依然存在。
-
嗯...您可以尝试其他选项,不要使用
View.GONE,而是根据需要添加/删除它们。这可能是最终迫使调整大小的原因。增加了开销,但并不比你已经尝试做的多得多。只需坚持Views的引用,您就不必重新膨胀它们。把它们放进去。
标签: android resize scrollview