Android 线性布局不在滚动视图内展开
我有一个Android 线性布局不在滚动视图内展开,android,android-layout,Android,Android Layout,我有一个LinearLayout在一个ScrollView中,它有android:layout\u height=“fill\u parent”,但它没有扩展到滚动视图的整个高度。我的布局看起来像: level layout layout_width layout_height 1 LinearLayout fill_parent fill_parent 2 LinearLayout fill_parent wrap_content 3
LinearLayout
在一个ScrollView
中,它有android:layout\u height=“fill\u parent”
,但它没有扩展到滚动视图的整个高度。我的布局看起来像:
level layout layout_width layout_height
1 LinearLayout fill_parent fill_parent
2 LinearLayout fill_parent wrap_content
3 (some irrelevant stuff)
2 ScrollView fill_parent fill_parent <-- this expands full height
3 LinearLayout fill_parent fill_parent <-- this does not (has orientation=vertical)
(following stuff probably are irrelevant, but just to be sure:)
4 TextView fill_parent fill_parent
4 LinearLayout fill_parent wrap_content
目前,我在有问题的LinearLayout
上使用了android:layout\u gravity=“bottom”
,这使得按钮无论如何都会粘在屏幕底部。但这也使文本粘在屏幕底部,这与我所追求的并不完全相同
更新:请注意,android:layout\u gravity=“bottom”
使滚动视图无法滚动。其他想法?能否提供布局xml?这样做可以让人们以最小的努力重现问题
你可能需要设置
android:layout_weight="1"
对于要展开的项目最终我自己找到了解决方案。问题不在于线性布局
,而在于滚动视图
(考虑到滚动视图
正在扩展,而线性布局
没有扩展,这似乎很奇怪)
解决方案是在滚动视图上使用android:fillViewport=“true”
我动态地使用它。我有一个线性布局,在这个使用滚动视图作为一个孩子。然后我再次获取LinearLayout并将任何视图添加到此LinearLayout,然后将此LinearLayout添加到ScrollView,最后将此ScrollView添加到LinearLayout。然后你可以在你的ScrollView中获得滚动,没有任何东西可以看到
LinearLayout(父级)--ScrollView(LinerLayout的子级)--LinearLayout(ScrollView的子级)--在此处添加文本视图、按钮、微调器等您想要的内容。然后将此LinearYout添加到ScrollView。Bcoz只有一个子项适用于ScrollView,最后将此ScrollView添加到LinearLyout。如果定义的区域超出屏幕大小,则u将在ScrollView中获得一个滚动。在我的情况下,我没有给出
LinearLayout的方向(ScrollView的子对象)
因此,默认情况下它是水平的,但scrollview是垂直的,因此请检查是否将“android:orientation=“vertical”设置为scrollview的子级(在LinearLayout的情况下)
这是我的案子希望它能帮助别人
.对于那些不想使用android:fillViewport=“true”
的人来说,我知道这篇文章非常古老,因为它有时不会在键盘上方显示编辑文本。
使用相对布局而不是线性布局解决了问题。解决方案是使用
android:fillViewport=“true”
在上滚动查看,并尝试使用
“包装内容”
而不是“填充父项”
为“填充父项”
现在已被弃用。这里的所有答案(完全)不适合我。
为了得到一个完整的答案,我们想做些什么:
我们有一个ScrollView,可以填充设备的视口,因此我们在布局xml中将fillViewport设置为“true”。
然后,在ScrollView中,我们有一个包含所有其他内容的LinearLayout,该LinearLayout应该至少与其父ScrollView一样高,因此应该位于(LinearLayout)底部的内容实际上,正如我们所希望的,位于屏幕底部(如果LinearLayout的内容比屏幕高,则在ScrollView的底部)
示例活动_main.xml布局:
<ScrollView
android:id="@+id/layout_scrollwrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_alignParentTop="true"
android:layout_above="@+id/layout_footer"
>
<LinearLayout
android:id="@+id/layout_scrollwrapper_inner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
...content which might or might not be higher than screen height...
</LinearLayout>
</ScrollView>
遗憾的是,这不是一个仅使用xml的解决方案,但它对我来说已经足够好了,希望它也能帮助其他饱受折磨的android开发人员在互联网上寻找解决方案;D谢谢你的回答。我尝试了你的建议,但没有奏效。我还更新了我的问题,将布局xml包括在内。请注意,LinearLayout将l垂直扩展,但这可能不一定会反映在您的布局中,除非它包含一个使用android:weight
消耗额外空间的控件。这真的很有帮助。谢谢。但如果没有此代码,有时效果会很好。你知道为什么吗?因为病得太重,我坐了好几个小时才开始搜索我的代码出了什么问题。这个参数是为了什么?你什么时候想将它设置为false?@Prasad使你的线性布局高度与parents匹配,但是这个解决方案使xml更加复杂,膨胀需要更多的时间。
<ScrollView
android:id="@+id/layout_scrollwrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_alignParentTop="true"
android:layout_above="@+id/layout_footer"
>
<LinearLayout
android:id="@+id/layout_scrollwrapper_inner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
...content which might or might not be higher than screen height...
</LinearLayout>
</ScrollView>
public class MainActivity extends AppCompatActivity {
@Override // Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayoutWrapper = findViewById(R.id.layout_scrollwrapper_inner);
...
linearLayoutWrapper.post(() -> {
linearLayoutWrapper.setMinimumHeight(((ScrollView)linearLayoutWrapper.getParent()).getHeight());
});
}
...
}