【问题标题】:Unwanted space created in the layout布局中创建了不需要的空间
【发布时间】:2018-04-16 18:02:31
【问题描述】:

我正在为我的应用程序开发一个 UI,它在尺寸比为 18:9 的所有设备上都可以正常工作,除了小米 18:9 设备。它在标签布局和状态栏之间创建了不需要的空间,如下所示:

我没有为 Tab 布局添加任何上边距或内边距。

我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout 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.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="0dp"
    android:id="@+id/tabCardView">

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/tabHeight"
        android:id="@+id/tabLayout"
        app:tabMode="fixed"
        android:background="?attr/tabBackgroundColor"/>

</android.support.v7.widget.CardView>

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/viewPager"
    android:background="?attr/homeBackgroundColor"
    android:layout_below="@+id/tabCardView"/>
</RelativeLayout>

我不知道为什么会这样。

【问题讨论】:

  • 尝试删除cardview

标签: android android-layout


【解决方案1】:

如果您将此元数据放入清单的标签中会发生什么? 很可能,这个设备使用的是 18.5:9 而不是普通的 18:9

<application ....  >
    <meta-data
        android:name="android.max_aspect"
        android:value="2.1"/>

</application>

更多信息在这里:https://android-developers.googleblog.com/2017/03/update-your-app-to-take-advantage-of.html

【讨论】:

    【解决方案2】:

    使用 CoordinatorLayout 作为父布局并设置 android:fitsSystemWindows="false"

    例如:

     <?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout 
      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.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="false">
    
    <android.support.v7.widget.CardView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:cardCornerRadius="0dp"
       android:id="@+id/tabCardView">
    
       <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/tabHeight"
        android:id="@+id/tabLayout"
        app:tabMode="fixed"
        android:background="?attr/tabBackgroundColor"/>
    
      </android.support.v7.widget.CardView>
    
      <android.support.v4.view.ViewPager
       android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/viewPager"
      android:background="?attr/homeBackgroundColor"
      android:layout_below="@+id/tabCardView"/>
    
      </CoordinatorLayout>
     </RelativeLayout>
    

    会有用的

    【讨论】:

    • 这没有任何意义。
    • 是的,我尝试了 Coodinatorlayout 和约束布局
    猜你喜欢
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-05
    相关资源
    最近更新 更多