Android : 在 ScrollView 中带有权重的 LinearLayout
Android : LinearLayout with weight inside a ScrollView
我的 ScrollView 遇到了这个令人沮丧的问题,这是我的层次结构:
ScrollView (Vertical)
LinearLayout (Vertical)
ImageView weight= 0.5
Whatever weight= 0.1
Whatever weight= 0.2
Whatever weight= 0.2
如果我删除 ScrollView(并让 LinearLayout 作为主要项目),这将适用于任何图像:图像占据屏幕尺寸的 50%,其他视图占据其余部分。
然而,当 ScrollView 在这里时,如果我的图像太高,"weight" 参数将被完全忽略:图像将尽其所能适应屏幕宽度,然后明显太高并占据屏幕的 50% 以上。编辑:实际上,所有 "weight" 属性似乎都被忽略了:
没有滚动视图:
使用滚动视图:
我希望线性布局无需滚动即可完美适应。那可能吗 ?我尝试更改一些图像选项(scaleType、adjustViewBounds),但我没有设法得到我想要的。
这是全部 xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:adjustViewBounds="true"
android:src="@drawable/testing" />
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="I'M A TEST" />
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:text="I'M A TEST" />
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="I'M A TEST" />
</LinearLayout>
</ScrollView>
注意:我需要 ScrollView,因为我使用的是 PullToRefresh ScrollView
试试这个方法。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:adjustViewBounds="true"
android:padding="50dp"
android:src="@drawable/ic_launcher" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="20dp"
android:text="I'M A TEST" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="20dp"
android:text="I'M A TEST" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="10dp"
android:text="I'M A TEST" />
</LinearLayout>
</ScrollView>
我通过添加一个父线性布局(和一个主要的相对布局)解决了这个问题:
ScrollView (Vertical)
LineaLayout
LinearLayout (Vertical)
ImageView weight= 0.5
Whatever weight= 0.1
Whatever weight= 0.2
Whatever weight= 0.2
并通过 childLinearLayout.getLayoutParams().height
以编程方式将子 LinearLayout 的高度设置为屏幕高度
也许为时已晚,但您的问题可以通过将 android:fillViewport="true"
添加到您的 ScrollView
来解决:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
我的 ScrollView 遇到了这个令人沮丧的问题,这是我的层次结构:
ScrollView (Vertical)
LinearLayout (Vertical)
ImageView weight= 0.5
Whatever weight= 0.1
Whatever weight= 0.2
Whatever weight= 0.2
如果我删除 ScrollView(并让 LinearLayout 作为主要项目),这将适用于任何图像:图像占据屏幕尺寸的 50%,其他视图占据其余部分。
然而,当 ScrollView 在这里时,如果我的图像太高,"weight" 参数将被完全忽略:图像将尽其所能适应屏幕宽度,然后明显太高并占据屏幕的 50% 以上。编辑:实际上,所有 "weight" 属性似乎都被忽略了:
没有滚动视图:
使用滚动视图:
我希望线性布局无需滚动即可完美适应。那可能吗 ?我尝试更改一些图像选项(scaleType、adjustViewBounds),但我没有设法得到我想要的。
这是全部 xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:adjustViewBounds="true"
android:src="@drawable/testing" />
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="I'M A TEST" />
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:text="I'M A TEST" />
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="I'M A TEST" />
</LinearLayout>
</ScrollView>
注意:我需要 ScrollView,因为我使用的是 PullToRefresh ScrollView
试试这个方法。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:adjustViewBounds="true"
android:padding="50dp"
android:src="@drawable/ic_launcher" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="20dp"
android:text="I'M A TEST" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="20dp"
android:text="I'M A TEST" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="10dp"
android:text="I'M A TEST" />
</LinearLayout>
</ScrollView>
我通过添加一个父线性布局(和一个主要的相对布局)解决了这个问题:
ScrollView (Vertical)
LineaLayout
LinearLayout (Vertical)
ImageView weight= 0.5
Whatever weight= 0.1
Whatever weight= 0.2
Whatever weight= 0.2
并通过 childLinearLayout.getLayoutParams().height
也许为时已晚,但您的问题可以通过将 android:fillViewport="true"
添加到您的 ScrollView
来解决:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >