简体   繁体   English

将布局添加到滚动视图会被吃掉

[英]Adding layout to a scrollview gets eaten up

I have a layout something like this: 我有这样的布局:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="10dp"
    android:background="#ff303f"
    android:id="@+id/layout">
    <ImageView 
        android:id="@+id/picture"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true"/>
    <Button
        android:id="@+id/cancel"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_alignRight="@id/picture"
        android:background="@drawable/cross_out"/>
</RelativeLayout>

I inflate it using the usual layout inflator service, set up some functionality to the button and an image to the imageview (all images are of same size and aspect ratio). 我使用通常的布局充气器服务为其充气,为按钮设置一些功能,并为图像视图设置图像(所有图像的大小和纵横比均相同)。

After that, I add this view to my fragment's layout which is nothing but a ScrollView which is a parent, it has a child linear layout that I call 'map' and simply add it to the map. 之后,我将此视图添加到片段的布局中,不过​​就是作为父级的ScrollView了,它具有子线性布局,我称之为“地图”,只需将其添加到地图即可。

Expected : The added layouts should get added properly and I can scroll through it. 预期:添加的布局应正确添加,我可以滚动浏览。 Actual : If more than 2 are added, the first one gets eaten up. 实际:如果添加的数量超过2,则第一个被吃掉。 I can see it half or sometimes it is completely eaten up. 我可以看到一半,有时甚至完全吃光了。 :\\ :\\

Any idea whats going on here? 知道这里发生了什么吗? Thanks a lot for your time. 非常感谢您的宝贵时间。

EDIT: Here's the layout file I add the inflated layout into: 编辑:这是我将膨胀后的布局添加到的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f6ff45">
        <LinearLayout
            android:id="@+id/map"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#43ff44"
            android:orientation="vertical"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:layout_marginTop="15dp">

</LinearLayout>
</ScrollView>

I also forgot to mention that after adding 3 or more of these layouts, I realized there's unnecessary empty space in the end. 我还忘了提一下,在添加了三个或更多这些布局之后,我意识到最后没有多余的空白空间。 :\\ :\\

If you have some android:layout_gravity or gravity property in your ScrollView, that may be the reason. 如果您在ScrollView中具有一些android:layout_gravitygravity属性,则可能是原因。 Try deleting it, or make it center_horizontal . 尝试将其删除,或将其center_horizontal

Try this code for your layout file; 尝试为您的布局文件使用此代码;

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f6ff45">
        <LinearLayout
            android:id="@+id/map"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#43ff44"
            android:orientation="vertical"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:layout_marginTop="15dp">

</LinearLayout>
</ScrollView>

As I mentioned before, android:layout_gravity="center" in this LinearLayout causes this problem. 如前所述,此LinearLayout中的android:layout_gravity="center"会导致此问题。 Because it doesn't only horizontally center but also verticelly center the contents. 因为它不仅使内容水平居中,而且也使内容垂直地居中。 It means when they are longer than the available height, the center part will appear. 这意味着当它们长于可用高度时,将出现中间部分。 I only changed it into android:layout_gravity="center_horizontal" and the problem is fixed. 我只将其更改为android:layout_gravity="center_horizontal" ,问题已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM