Java Android:滚动视图中带有权重的线性布局
我的ScrollView有一个令人沮丧的问题,下面是我的层次结构:Java Android:滚动视图中带有权重的线性布局,java,android,xml,scrollview,android-linearlayout,Java,Android,Xml,Scrollview,Android Linearlayout,我的ScrollView有一个令人沮丧的问题,下面是我的层次结构: ScrollView (Vertical) LinearLayout (Vertical) ImageView weight= 0.5 Whatever weight= 0.1 Whatever weight= 0.2 Whatever weight= 0.2 如果我删除了ScrollView(并将LinearLayout作为主项),这对任何图像都能正常工
ScrollView (Vertical)
LinearLayout (Vertical)
ImageView weight= 0.5
Whatever weight= 0.1
Whatever weight= 0.2
Whatever weight= 0.2
如果我删除了ScrollView(并将LinearLayout作为主项),这对任何图像都能正常工作:图像占据屏幕大小的50%,其他视图占据其余部分
但是,当ScrollView在这里时,如果我的图像太高,“权重”参数将被完全忽略:图像将尽其所能适应屏幕宽度,然后将明显太高,占据屏幕的50%以上。编辑:实际上,所有“权重”属性似乎都被忽略了:
没有滚动视图:
使用ScrollView:
我希望线性布局完全适合,而不必滚动。可能吗?我曾尝试更改一些图像选项(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>
我通过添加一个父线性布局(和一个主相对布局)解决了这个问题:
使用
childLinearLayout.getLayoutParams().height以编程方式将子LinearLayout的高度设置为屏幕的高度可能为时已晚,但您的问题可以通过将android:fillViewport=“true”
添加到滚动视图中来解决:
<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没有维度,它可以是无限的。您希望imageview的50%是什么?如果是线性布局,它就知道了。ScrollView会占用所需的空间。这似乎不对。看一看,这可能是您所需要的。如果您希望将布局分组到ScrollView中进行修复,那么将布局分组没有任何意义。我需要此ScrollView,因为我正在使用ScrollView使用PullToRefresh库。我希望我的图像是屏幕大小的50%,而用户还没有滚动。但这不符合50%,20%,20%,10%的高度要求。真的。。。wrap content
将如何尊重百分比?了解当前行为将是非常好的。我同意了,这个解决方案奏效了!
<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" >