【问题标题】:Android toolbar is covering a fragmentAndroid工具栏覆盖了一个片段
【发布时间】:2016-02-28 02:33:21
【问题描述】:

我在一个应用程序中有 2 个布局被工具栏覆盖。在每个布局中,我都尝试过使用android:layout_below="@id/app_bar_layout 或android:layout_below="@id/toolbar,但都没有运气。我怎样才能解决这个问题?

通过触摸菜单项调用回收器视图,并由 rv adpater 和视图持有者类扩展。到目前为止,除了覆盖视图的工具栏外,我的应用程序没有其他问题。

<?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>

RV适配器:

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 的结果。一个空白栏覆盖了刚刚动画化的文本视图。

【问题讨论】:

    标签: android android-layout android-actionbar android-toolbar


    【解决方案1】:

    您必须在 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 会导致新可见的文本视图被屏幕底部的空格覆盖。我将在原始回复中添加更多代码。
    • 您不必将app:layout_behavior 添加到您的Card 布局中。只有content_main.xml 文件中的父视图
    • 糟糕——我把它放在那里作为一个实验。但是,我确实在您建议的地方添加了行为线,但它仍然没有将视图向下推。视图仍然被工具栏遮挡。
    【解决方案2】:

    您的工具栏高度定义为:android:layout_height="?attr/actionBarSize" 所以我在列表视图中添加了带有这些值的上边距: android:layout_marginTop="?attr/actionBarSize"

    在我正在开发的应用中为我工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多