【问题标题】:How to remove this gap from the Android layout如何从 Android 布局中消除这种差距
【发布时间】:2016-01-19 09:15:14
【问题描述】:

我尽我所能消除布局底部的这个间隙 (shown in the screenshot below),但结果却是失望!

这里是tournament_home.xml的xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background">

<include
    android:id="@+id/toolbar_tournament_home"
    layout="@layout/toolbar_tournament_home"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentTop="true" />


<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar_tournament_home">

        <LinearLayout
            android:id="@+id/lnl_home_screen"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/frame_home_screen_tournament_name"
            android:orientation="vertical">

            <ab9.com.cricliveptc.tabs.SlidingTabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/black"
                android:elevation="2dp" />

            <View
                android:id="@+id/toolbar_shadow"
                android:layout_width="match_parent"
                android:layout_height="@dimen/margin2x"
                android:background="@drawable/toolbar_dropshadow" />

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginTop="-4dp"
                android:layout_weight="1" />
        </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="270dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#FFFFFF"
        android:scrollbars="vertical" />
</android.support.v4.widget.DrawerLayout>

这是屏幕截图,您可以清楚地看到屏幕底部留下一个小的水平间隙。

如何解决?

【问题讨论】:

  • 你检查过父Relative Layout的margin和padding设置吗?
  • @Abhi 是的,我做到了。我没有对父布局应用任何边距和/或填充。
  • 尝试删除视图寻呼机的负填充
  • @VivekMishra,试过了。它将布局略微向下移动,但不完全移动。此外,我已将android:layout_height="0dp" and android:layout_weight="1" 应用于 ViewPager,这使得(应该)它占据整个屏幕,而与负填充无关。
  • 删除android:layout_marginTop="-4dp"(猜测)。

标签: android android-layout android-xml drawerlayout


【解决方案1】:

从您的布局中删除 android:layout_marginTop="-4dp"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background">

    <include
        android:id="@+id/toolbar_tournament_home"
        layout="@layout/toolbar_tournament_home"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentTop="true" />


    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar_tournament_home">

        <LinearLayout
            android:id="@+id/lnl_home_screen"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/frame_home_screen_tournament_name"
            android:orientation="vertical">

            <ab9.com.cricliveptc.tabs.SlidingTabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/black"
                android:elevation="2dp" />

            <View
                android:id="@+id/toolbar_shadow"
                android:layout_width="match_parent"
                android:layout_height="@dimen/margin2x"
                android:background="@drawable/toolbar_dropshadow" />

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="270dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="#FFFFFF"
            android:scrollbars="vertical" />
    </android.support.v4.widget.DrawerLayout>
    </RelativeLayout>

【讨论】:

    【解决方案2】:

    http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html

    DrawerLayout 充当顶层容器 允许从边缘拉出交互式“抽屉”视图 窗口。

    您不应将 RelativeLayout 用作顶级。在您的内容中添加 Toolbar 可能是这样的:

        <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar_tournament_home">
    
        <LinearLayout
            android:id="@+id/lnl_home_screen"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/frame_home_screen_tournament_name"
            android:orientation="vertical">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbarmain"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/white"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
    
            <ab9.com.cricliveptc.tabs.SlidingTabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/black"
                android:elevation="2dp" />
    
            <View
                android:id="@+id/toolbar_shadow"
                android:layout_width="match_parent"
                android:layout_height="@dimen/margin2x"
                android:background="@drawable/toolbar_dropshadow" />
    
            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
        </LinearLayout>
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="270dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="#FFFFFF"
            android:scrollbars="vertical" />
    
        <!-- this is not a good place -->
    </android.support.v4.widget.DrawerLayout>
    

    另外,我现在更愿意使用CoordinatorLayoutSupportLibrary

    还有一件事,我猜你的设计是完全错误的,RecyclerView 会像 NavigationDrawer 而这不是 android 文档所说的。

    您可能还想看看这里:http://developer.android.com/training/implementing-navigation/nav-drawer.html

    【讨论】:

    • 您的答案仅与抽屉布局有关。我说过,每当我在图形编辑器中选择任何根布局时,它都会在最后显示出轻微的间隙
    • 你试过了吗?:android:layout_height="wrap_content"
    【解决方案3】:

    删除android:layout_marginTop="-4dp",正如其他人所说。我猜你的意思是android:layout_marginTop="4dp"反正。

    如果您想在 ViewViewPager 之间留出边距,只需为 View 元素设置底部边距。

    原因是 android:layout_height="0dp" and android:layout_weight="1" 正在工作,但是您设置了一个负的上边距,它正在向上移动元素。

    【讨论】:

      【解决方案4】:

      尝试删除viewpagerandroid:layout_weight="1" 并将android:layout_height="0dp" 替换为android:layout_height="match_parent"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-09
        • 1970-01-01
        • 1970-01-01
        • 2014-09-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多