导航抽屉活动与我的内容重叠

时间:2017-02-18 06:28:35

标签: android xml android-fragments

我为我的项目选择了导航抽屉活动。

我打算在这个项目中使用Fragments。当我尝试显示我的片段时,工具栏会重叠到我的片段内容上。

我很确定这是我的xml的问题,但不知道在哪里。 activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            layout="@layout/content_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>


    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

我的content_main.xml包含一个空框架布局,因此我可以使用事务添加片段。我的片段内容显示,但顶部部分被工具栏隐藏。

3 个答案:

答案 0 :(得分:2)

尝试将其排列为“content_main.xml”(包含您的片段) container)在“app_bar_main.xml”中“include -ed”。

代码:

  

activity_main.xml中

My computer configuration : 

Main Memory : 2GB

processor : Intel Quad core processor ( up to 2.66 GHz)
  

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- Created for MainActivity -->
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>
  

content_main.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- Created for MainActivity -->
<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=".MainActivity">

    <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_main" />

    <!--
    <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"
        app:srcCompat="@android:drawable/ic_dialog_email" />
    -->

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

答案 1 :(得分:1)

在app_bar_main.xml中包含内容main,如下所示:

int digit1 = 5;
int digit2 = number;

答案 2 :(得分:0)

这里以新的布局编写您希望在打开应用时显示默认值的代码并设置其 visibility =&#34; visible&#34; !!

content_main.xml

    <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context=".MainActivity">

            <LinearLayout
                android:id="@+id/strtlayID"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"

 android:visibility="visible">
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:src="@drawable/canada"
                    android:layout_marginTop="40dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:textSize="20dp"
                    android:textStyle="bold"
                    android:text="This is safety application for women's. "/>
            </LinearLayout>
            <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >

                </FrameLayout>

        </RelativeLayout>

此处设置布局 li.setVisibility(View.INVISIBLE); ,其中u获取所选片段的ID!

MainActivity.java

    public class MainActivity extends AppCompatActivity
                implements NavigationView.OnNavigationItemSelectedListener {
            Fragment fragment,ff=null;
            LinearLayout li;
        int pre;
            Class fragmentClass = null;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);
                li=(LinearLayout)findViewById(R.id.strtlayID);

                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();


                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
                drawer.setDrawerListener(toggle);
                toggle.syncState();

                NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
                navigationView.setNavigationItemSelectedListener(this);

            }

            @Override
            public void onBackPressed() {
                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                if (drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(GravityCompat.START);
                } else {
                    super.onBackPressed();
                }
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();

                //noinspection SimplifiableIfStatement
                if (id == R.id.action_settings) {
                    return true;
                }

                return super.onOptionsItemSelected(item);
            }
            private void displaySelectedScreen(int itemId) {
                Fragment fragment = null;



li.setVisibility(View.INVISIBLE);

                switch (itemId) {
                    case R.id.nav_start:

                        fragment = new start_app();
                        pre=R.id.nav_start;
                        break;
                    case R.id.nav_word:
                        fragment = new word_act();
                        break;
                    case R.id.nav_setting:
                        fragment = new setting();
                        break;
                    case R.id.nav_faq:
                        fragment = new Faq();
                        break;
                    case R.id.nav_logout:
                        fragment = new Logout();
                        break;
                    case R.id.nav_share:
                        fragment = new Share();
                        break;
                    case R.id.nav_send:
                        fragment = new Sens();
                        break;
                }
                if (fragment != null) {
                    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    //ft.setCustomAnimations(R.anim.enter,R.anim.exit,R.anim.pop_enter,R.anim.pop_exit);
                    ft.replace(R.id.content_frame, fragment);
                    ft.commit();
                }


                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                drawer.closeDrawer(GravityCompat.START);
            }
            @SuppressWarnings("StatementWithEmptyBody")
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                displaySelectedScreen(item.getItemId());

                return true;
            }
        }
相关问题