我想为ScrollView
内LinearLayout
内的多个项目分配布局权重。但是,ScrollView
忽略了LinearLayout
weightSum
。
我的目标是将布局的权重划分为 2、1、1(总和为 4),但这在ScrollView
内无法正常工作。
如何解决此布局问题?
主.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="4">
<LinearLayout android:id="@+id/logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="2"
android:background="#FFFFFF" />
<LinearLayout android:id="@+id/logo1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:background="#000000" />
<LinearLayout android:id="@+id/logobutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:background="#4B4B4B" />
</LinearLayout>
</ScrollView>
我以前遇到过这个问题。只需在ScrollView中使用android:fillViewport="true"
,它就会填满屏幕。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
这不会像您所做的那样工作。滚动视图的子视图应设置为 wrap_content
。如果将其设置为 fill_parent
,它将填充 ScrollView 的区域,并且永远不会滚动,因为它不会大于 ScrollView。
layout_weight
的想法是按比例填充特定区域。
您应该将所有子 LinearLayouts layout_height
设置为wrap_content
或特定大小(以 dp 为单位),并将父 LinearLayout layout_height
设置为 wrap_content
如前所述,您需要删除额外的
xmlns:android="http://schemas.android.com/apk/res/android"
如果它不是布局的根(第一个)元素。
只需将其放在滚动视图中:
android:fillViewport="true"
根据我的经验,fillviewport=true
工作有点奇怪。
我通过使用另一个布局(如 FrameLayout
)包装LinearLayout
来解决这个问题。
我希望这有所帮助。
在滚动视图中添加以下行,它将正常工作。
只有单个孩子必须在那里滚动视图
android:fillViewport="true"