【问题标题】:AppBarLayout Cuts the Top of the First RecyclerView ItemAppBarLayout 切掉第一个 RecyclerView 项的顶部
【发布时间】:2018-06-23 08:24:19
【问题描述】:

我有一个 appbarlayout 和 recyclerview 的活动。当我运行我的代码时,第一个 recyclerview 项目的顶部被切断,看起来像这样:

我已经搜索了解决方案,但他们中的大多数人都说使用这行代码:app:layout_behavior="@string/appbar_scrolling_view_behavior,由于某种原因,这对我不起作用。

这是我的 contacts.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".navdraweractivities.ContactsListActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:popupTheme="@style/AppTheme.PopupOverlay">

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/contacts_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?attr/actionBarSize"
        android:clipToPadding="false"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>

编辑:

Napster 的回答是这样的:

编辑 2:

我使用this教程添加半透明状态栏,添加后我有recyclerview填满整个屏幕,第一个项目出现在状态栏后面:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    setContentView(R.layout.navdrawer_contacts_list);
    setStatusBarTranslucent(true);
    ...

protected void setStatusBarTranslucent(boolean makeTranslucent) {

    Rect rectangle = new Rect();
    Window window = getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
    int statusBarHeight = rectangle.top;
    int contentViewTop =
            window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
    int titleBarHeight= contentViewTop - statusBarHeight;

    View v = findViewById(R.id.appbar);
    if (v != null) {
        int paddingTop = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? getStatusBarHeight() : 0;
        TypedValue tv = new TypedValue();
        getTheme().resolveAttribute(android.support.v7.appcompat.R.attr.actionBarSize, tv, true);
        paddingTop += TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
        v.setPadding(0, makeTranslucent ? paddingTop : 0, 0, 0);
    }

    Log.i("*** Elenasys :: ", "StatusBar Height= " + statusBarHeight + " , TitleBar Height = " + titleBarHeight);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        if (makeTranslucent) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }
}

public int getStatusBarHeight() {
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}
}

我更新的 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/bellow_actionbar"   
    android:layout_height="match_parent"
    tools:context=".navdraweractivities.ContactsListActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:popupTheme="@style/AppTheme.PopupOverlay">

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

    <android.support.v7.widget.RecyclerView    
        android:id="@+id/contacts_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

但我还是把第一个 recyclerview 项目的顶部截掉了。

【问题讨论】:

    标签: android xml android-recyclerview android-relativelayout android-appbarlayout


    【解决方案1】:

    将主根布局从RelativeLayout 转换为CoordinatorLayout。这就是你的布局的样子

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 
      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"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      tools:context=".navdraweractivities.ContactsListActivity">
    
      <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:popupTheme="@style/AppTheme.PopupOverlay">
    
      </android.support.design.widget.AppBarLayout>
    
      <android.support.v7.widget.RecyclerView
        android:id="@+id/contacts_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </android.support.design.widget.CoordinatorLayout>
    

    并从RelativeLayout 中删除android:paddingTop="?attr/actionBarSize" 这一行以删除多余的空间

    【讨论】:

    • 我试过这个,但我得到的空间太大了。我在我的问题中添加了另一个图像。
    • 去掉 android:paddingTop="?attr/actionBarSize" 这行去掉多余的空格
    • 当我删除那行代码时,我又得到了第一个图像......第一个项目的顶部被切断了
    • 有了CoordinatorLayout 不是没有它
    • 是的,使用 coordinatorLayout
    【解决方案2】:

    我玩过,现在可以了。我仍然不确定为什么它从一开始就不起作用。

    这就是现在的样子:

    联系人活动:

    super.onCreate(savedInstanceState);
        setContentView(R.layout.navdrawer_contacts_list);
        setStatusBarTranslucent(true);
    
        // show dialog
        pDialog = new ProgressDialog(this);
        pDialog.setMessage(getString(R.string.loading));
        pDialog.show();
    
        toolbar = findViewById(R.id.contactsListToolbar);
        setSupportActionBar(toolbar);
    
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    

    联系人xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".navdraweractivities.ContactsListActivity">
    
    <android.support.v7.widget.Toolbar
        android:id="@+id/contactsListToolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/ppdColorOrange"
        android:fitsSystemWindows="true"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ToolbarWhiteBackArrow">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:id="@+id/contactsListTextView"
                style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/contacts"
                android:textColor="@color/white" />
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>
    
    <android.support.v7.widget.RecyclerView
        android:id="@+id/contacts_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_row_selector" />
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多