安卓XML截取布局底部一半

12

我的XML布局出了问题,这是我认为不会有太多问题的地方。我在一个滚动视图中有以下布局,但底部的布局被截断了,我无法看到第二个列表之后的任何内容。从周围看来,我似乎没有找到XML本身的问题,也不知道自己出了什么问题。

我尝试了解决问题的建议,即为每个不同元素添加权重,但这仍然没有解决问题。

我还添加了主要活动,其中包含片段,以便可能有助于解决问题。

片段XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinatorLayout">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:scrollbars="vertical">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <android.support.design.widget.TextInputLayout
            android:id="@+id/text_input_layout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <EditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Recipe Title"/>
        </android.support.design.widget.TextInputLayout>

        <TextView
            android:id="@+id/ingredientsHeading"
            android:layout_below="@+id/text_input_layout"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:text="Ingredients"
            android:textStyle="bold|italic"
            android:layout_weight="1" />
        <ListView
            android:id="@+id/ingredientsList"
            android:layout_below="@+id/ingredientsHeading"
            android:layout_above="@+id/directionsHeading"
            android:layout_width="wrap_content"
            android:layout_height="195dp"
            android:layout_weight="1"></ListView>

        <Button style="@style/Button"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:text="Add Ingredient"
            android:id="@+id/addIngredient"
            android:layout_below="@+id/ingredientsList"
            android:enabled="true"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/directionsHeading"
            android:layout_below="@+id/addIngredient"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:text="Directions"
            android:textStyle="bold|italic"
            android:layout_weight="1" />
        <ListView
            android:id="@+id/directionsList"
            android:layout_below="@+id/directionsHeading"
            android:layout_width="wrap_content"
            android:layout_height="195dp"
            android:layout_weight="1"></ListView>

        <Button style="@style/Button"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:text="Add Direction"
            android:id="@+id/addDirection"
            android:layout_below="@+id/ingredientsList"
            android:enabled="true"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1" />

    </LinearLayout>

</ScrollView>


<android.support.design.widget.FloatingActionButton
    android:id="@+id/filterButton"
    app:backgroundTint="@color/floatingButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:clickable="true"
    android:src="@drawable/ic_filter"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="63dp"
    android:layout_marginRight="16dp" />


</android.support.design.widget.CoordinatorLayout>

主 XML

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout2">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <FrameLayout
        android:id="@+id/container_body"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</android.support.design.widget.CoordinatorLayout>


<fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="com.example.rory.pocketchef.Fragments.FragmentDrawer"
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

2
please post a screenshot... - Elvis Chweya
你不应该使用嵌套权重。Android Studio 应该已经警告了你,而且权重也不能超过 weightsum。 - Vivek Mishra
4个回答

7
我解决了这个问题。由于滚动视图在实际手机上显示时底部被底部操作栏遮盖,所以我在滚动视图底部添加了填充以将其推回到操作栏上方,从而解决了这个问题。
新的布局如下: 更新后的可用XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinatorLayout">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:scrollbars="vertical"
    android:paddingBottom="?android:attr/actionBarSize"> <<<-------added this line
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <android.support.design.widget.TextInputLayout
            android:id="@+id/text_input_layout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <EditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Recipe Title"/>
        </android.support.design.widget.TextInputLayout>

        <TextView
            android:id="@+id/ingredientsHeading"
            android:layout_below="@+id/text_input_layout"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:text="Ingredients"
            android:textStyle="bold|italic"
            android:layout_weight="1" />
        <ListView
            android:id="@+id/ingredientsList"
            android:layout_below="@+id/ingredientsHeading"
            android:layout_above="@+id/directionsHeading"
            android:layout_width="wrap_content"
            android:layout_height="195dp"
            android:layout_weight="1"></ListView>

        <Button style="@style/Button"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:text="Add Ingredient"
            android:id="@+id/addIngredient"
            android:layout_below="@+id/ingredientsList"
            android:enabled="true"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/directionsHeading"
            android:layout_below="@+id/addIngredient"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:text="Directions"
            android:textStyle="bold|italic"
            android:layout_weight="1" />
        <ListView
            android:id="@+id/directionsList"
            android:layout_below="@+id/directionsHeading"
            android:layout_width="wrap_content"
            android:layout_height="195dp"
            android:layout_weight="1"></ListView>

        <Button style="@style/Button"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:text="Add Direction"
            android:id="@+id/addDirection"
            android:layout_below="@+id/ingredientsList"
            android:enabled="true"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_below="@+id/addDirection">

            <Button style="@style/Button"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:text="Add Direction"
                android:id="@+id/showOptionsDialog"
                android:enabled="true"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1" />

            <Button style="@style/Button"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:text="Add Direction"
                android:id="@+id/saveRecipe"
                android:enabled="true"
                android:layout_gravity="center_horizontal"
                android:layout_toRightOf="@+id/showOptionsDialog"
                android:layout_weight="1" />


        </RelativeLayout>

    </LinearLayout>

</ScrollView>


<android.support.design.widget.FloatingActionButton
    android:id="@+id/filterButton"
    app:backgroundTint="@color/floatingButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:clickable="true"
    android:src="@drawable/ic_filter"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="63dp"
    android:layout_marginRight="16dp" />


</android.support.design.widget.CoordinatorLayout>

这会在底部提供太多的填充,相反我们可以在主XML文件的底部放置一个高度为2dp的不可见视图,并告诉滚动视图在该视图之上。然后也可以解决这个问题。 - frogEye

4

在您的xml文件中为所有组件使用weightSum

它是android:layout_weight。Weight只能在LinearLayout中使用。如果线性布局的方向是垂直,则使用android:layout_height="0dp",如果方向是水平,则使用android:layout_width="0dp"。 这样可以完美地工作。

根据您的问题-布局底部被裁剪

这是因为给组件设置了固定高度。

编辑-添加了xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp"
    tools:context="info.androidhive.materialtabs.fragments.OneFragment">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fadingEdge="none"
        android:fillViewport="true"
        android:isScrollContainer="true"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="10">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/text_input_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/editText"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Recipe Title" />
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="3.5"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/ingredientsHeading"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:text="Ingredients"
                    android:textStyle="bold|italic" />

                <ListView
                    android:id="@+id/ingredientsList"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center">

                <Button
                    android:id="@+id/addIngredient"
                    style="@style/AppTheme"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:enabled="true"
                    android:text="Add Ingredient" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="3.5"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/directionsHeading"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:text="Directions"
                    android:textStyle="bold|italic" />

                <ListView
                    android:id="@+id/directionsList"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center">

                <Button
                    android:id="@+id/addDirection"
                    style="@style/AppBaseTheme"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:enabled="true"
                    android:text="Add Direction" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

我应该为XML中的所有元素更改android:layout_height吗? - JJSmith
如果您根据android:layout_weight更改了xml,则需要更改所有组件的高度。 - Amit Vaghela
谢谢你提供的XML文件,但是它仍然出现相同的错误。现在我无法滚动页面,布局也无法滚动,与之前只能滚动一小部分的情况不同。 - JJSmith
你的ListView里有数据吗? - Amit Vaghela
我的问题仍未解决,我会在找到解决方案后再接受。 - JJSmith
显示剩余5条评论

4

将以下代码添加到你的ScrollView中

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"

android:scrollbars="vertical">

仍然是同样的问题 - JJSmith
将ScrollView作为您的布局父级,就像下面的代码一样 - Rahul Chaudhary
<?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" android:fillViewport="true" android:scrollbarStyle="insideInset" android:scrollbars="vertical"> </ScrollView> - Rahul Chaudhary

2
您将scrollviewweightsum设置为1,但您需要像这样设置<LinearLayout weightsum=15>(您不必设置为15),然后您需要在布局中分配您的15总和。例如,<TextView layout_weight=1>表示您的textview将占用1/15。当我使用权重时,我还将高度或宽度设置为0dp,具体取决于我想要调整的哪个尺寸。例如,如果我想调整宽度,则可以使用<TextView width=0dp height=wrap_content weight=1>。因此,您需要为LinearLayout的每个子元素分配一个权重。
编辑:同时,在ScrollView中使用ListView并不是一个好主意,可能会导致问题。请参考以下帖子:如何在不使其折叠的情况下将ListView放入ScrollView中?

1
将以下这些行添加到您的ScrollView中。 - Rahul Chaudhary

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接