AppBarLayout削减了第一个RecyclerView项目的顶部

时间:2018-01-14 14:28:08

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

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

enter image description here

我搜索了一个解决方案,但大多数人都说使用这行代码: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的回答是:

enter image description here

编辑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项目的顶部切断了。

2 个答案:

答案 0 :(得分:0)

将主根布局从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>

android:paddingTop="?attr/actionBarSize"移除RelativeLayout此行以删除额外空格

答案 1 :(得分:0)

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

这就是现在的样子:

通讯录活动:

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>