安卓工具栏覆盖了一个片段。

3

我在应用程序中有2个布局被工具栏覆盖。在每个布局中,我尝试使用android:layout_below="@id/app_bar_layoutandroid:layout_below="@id/toolbar,但没有成功。我该如何解决这个问题?

Recycler视图是通过菜单项的触摸调用的,并由rv adapter和view holder类膨胀。到目前为止,我在应用程序中没有其他问题,除了工具栏遮盖了视图。

<?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:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.aaron.walkingtourtest09feb.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/app_bar_layout"
    android:theme="@android:style/Theme.Material">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:elevation="4dp"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        android:background="#607D8B"/>

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

    <include layout="@layout/content_main"/>

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

content_main.xml:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    tools:context="com.example.test09feb.MainActivity"
    tools:showIn="@layout/app_bar_main"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

<fragment
    android:id="@+id/listFragment"
    android:tag="unique_tag"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.example.test09feb.MainActivity$ExampleFragment" >
</fragment>

Recyclerview_activity.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/cardview_light_background"
android:animateLayoutChanges="true"
android:padding="6dp">

<android.support.v7.widget.RecyclerView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/rv">
</android.support.v7.widget.RecyclerView>

cards.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/tools"
          xmlns:card_view="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical">


<android.support.v7.widget.CardView
    android:id="@+id/cv"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="4dp"
    card_view:cardUseCompatPadding="true">

    <RelativeLayout
        android:id="@+id/card_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:background="#EEEEEE"
        android:orientation="vertical"
        android:padding="0dp">

        <ImageView
            android:id="@+id/subject_photo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"/>

        <TextView
            android:id="@+id/subject_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/subject_photo"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="16dp"
            android:textColor="@android:color/black"
            android:textSize="16sp"/>

        <TextView
            android:id="@+id/subject_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/subject_name"
            android:paddingBottom="16dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:textColor="#777777"
            />

        <Button
            android:id="@+id/card_button_left"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/subject_text"
            android:layout_toLeftOf="@+id/card_button_right"
            android:text="@string/watch"
            />

        <Button
            android:id="@id/card_button_right"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@id/subject_text"
            android:text="@string/read"/>

        <TextView
            android:id="@+id/expanded_subject_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/card_button_right"
            android:paddingBottom="16dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:textColor="#777777"
            />
    </RelativeLayout>

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

RVAdapter:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.SubjectViewHolder> {

List<Subject> subjects;
static SubjectViewHolder svh;
View v = null;
Context cxt;

private static MyListener listener;

public interface MyListener {
    void onClick(View itemView, int viewPosition);

}


RVAdapter(List<Subject> subjects, Context cxt) {
    this.subjects = subjects;
    this.cxt = cxt;
}

public void setOnClickListener(MyListener listener) {
    this.listener = listener;
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

@Override
public SubjectViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards, viewGroup, false);
    svh = new SubjectViewHolder(v);
    return svh;
}

String[] localLinks = {
        "http://192.168.11.111:8000/pic1.png",
        "http://192.168.11.111:8000/pic2.jpg",
        "http://192.168.11.111:8000/pic3.png",
        "http://192.168.11.111:8000/pic4.jpg",
        "http://192.168.11.111:8000/pic5.png",
        "http://192.168.11.111:8000/pic6.jpg",
        "http://192.168.11.111:8000/pic7.png",
        "http://192.168.11.111:8000/pic8.jpg",
        "http://192.168.11.111:8000/pic9.png",
        "http://192.168.11.111:8000/pic10.jpg",
        "http://192.168.11.111:8000/pic11.png",
        "http://192.168.11.111:8000/pic12.png",
        "http://192.168.11.111:8000/pic13.png",
        "http://192.168.11.111:8000/pic14.png",
};


@Override
public void onBindViewHolder(SubjectViewHolder subjectViewHolder, int i) {
    subjectViewHolder.subjectName.setText(subjects.get(i).subjectName);
    subjectViewHolder.subjectText.setText(subjects.get(i).subjectText);
    Picasso.with(cxt).load(localLinks[i]).into(subjectViewHolder.subjectPhoto);
    subjectViewHolder.expandedSubjectText.setText(subjects.get(i).expandedSubjectText);
    subjectViewHolder.expandedSubjectText.setVisibility(View.GONE);


}

@Override
public long getItemId(int position) {
    return position;
}


@Override
public int getItemCount() {
    return subjects.size();
}

public static class SubjectViewHolder extends RecyclerView.ViewHolder {

    CardView cv;
    TextView subjectName;
    TextView subjectText;
    TextView expandedSubjectText;
    ImageView subjectPhoto;
    Button leftButton;
    Button rightButton;


    SubjectViewHolder(final View itemView) {

        super(itemView);

        cv = (CardView) itemView.findViewById(R.id.cv);
        subjectName = (TextView) itemView.findViewById(R.id.subject_name);
        subjectText = (TextView) itemView.findViewById(R.id.subject_text);
        expandedSubjectText = (TextView) itemView.findViewById(R.id.expanded_subject_text);
        subjectPhoto = (ImageView) itemView.findViewById(R.id.subject_photo);
        leftButton = (Button) itemView.findViewById(R.id.card_button_left);
        rightButton = (Button) itemView.findViewById(R.id.card_button_right);

        rightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (expandedSubjectText.getVisibility() == View.GONE) {
                    expandedSubjectText.setVisibility(View.VISIBLE);
                } else {
                    expandedSubjectText.setVisibility(View.GONE);
                }
                // Triggers click upwards to the adapter on click
                if (listener != null)
                    v.findViewById(R.id.card_container);
                listener.onClick(rightButton, svh.getAdapterPosition());
            }
        });

        leftButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Triggers click upwards to the adapter on click
                if (listener != null)
                    listener.onClick(leftButton, svh.getAdapterPosition());
            }
        });
    }
}}

按照建议添加appbar_scrolling_view_behavior的结果。一个空白的栏覆盖了刚刚动画显示出来的textview。

在这里输入图片描述

2个回答

11
你需要在content_main.xml文件中的父视图中添加以下属性。
app:layout_behavior="@string/appbar_scrolling_view_behavior"

就像这样:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.test09feb.MainActivity"
    tools:showIn="@layout/app_bar_main"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

</FrameLayout>

谢谢。我忘了提到我已经尝试过这种方法,但它会破坏我的recyclerview中卡片视图的布局。每个卡片都有一个“阅读”按钮,可以使文本视图可见。添加appbar_scrolling_view_behavior会导致新出现的文本视图被屏幕底部的空白覆盖。我将在我的原始响应中添加更多代码。 - user465001
您不必将 app:layout_behavior 添加到您的 Card 布局中。只需在 content_main.xml 文件中的父视图中添加即可。 - Sharjeel
糟糕 - 我把那个放在那里只是作为一个实验。不过,我确实按照你建议的添加了行为代码,但视图仍然没有被推下来。视图仍然被工具栏遮挡着。 - user465001

6

您的工具栏高度由以下代码定义:android:layout_height="?attr/actionBarSize", 因此我将这些值添加到列表视图的顶部边距中: android:layout_marginTop="?attr/actionBarSize"

在我正在开发的应用程序中,该方法有效。


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