片段布局重叠主要活动的布局

时间:2016-02-25 06:12:08

标签: android android-fragments android-activity

主要活动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:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.apuroopa.homework5_apuroopa.ActionBar_Activity"
    android:id="@+id/container">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

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

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

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

/// cardview layout xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_margin="5dp"
    >


    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#fffffff0"
        >


        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:layout_toRightOf="@+id/selection"

            android:layout_marginLeft="10dp"
            />
        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:layout_toLeftOf="@+id/image"

            android:layout_marginLeft="10dp"/>

        <CheckBox
            android:id="@+id/selection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:clickable="false"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignTop="@id/title"
            />
        <ImageView
            android:id="@+id/image"
            android:layout_width="80dp"
            android:layout_height="100dp"
            android:layout_alignParentRight="true"
            android:scaleType="fitXY"/>
    </RelativeLayout>

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

/// enter image description here

 <?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.apuroopa.homework5_apuroopa.ActionBarFragment">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

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

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

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/cardList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</android.support.design.widget.CoordinatorLayout>


//Main Activity java 

public class ActionBar_Activity extends AppCompatActivity implements Fragment_RecyclerView.OnListItemSelectedListener
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_bar_);
        Fragment mcontent =  Fragment_RecyclerView.newInstance(0);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar actionBar=getSupportActionBar();
        Fragment_RecyclerView FR = new Fragment_RecyclerView();
        getSupportFragmentManager().beginTransaction().add(R.id.container,mcontent)
                //(Fragment_RecyclerView.newInstance(0)," ")
                .commit();
    }

我有一个在活动中膨胀的菜单文件。活动中会添加Recyclerview fragment。 recyclerview片段中的每一行都包含cardview布局。我的recyclerview与菜单选项重叠,如图所示。工具栏位于布局文件中的recyclerview上方。

3 个答案:

答案 0 :(得分:1)

在cardview代码中添加app:layout_behavior="@string/appbar_scrolling_view_behavior"

答案 1 :(得分:0)

将CardView容器放入RelativeLayout:

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

答案 2 :(得分:0)

首先给android.support.design.widget.AppBarLayout

一个id

然后

<android.support.v7.widget.RecyclerView
    android:layout_below=@id/app_bar_layout_id/>