2017-02-18 106 views
-1

我爲我的項目選擇了導航抽屜活動。導航抽屜活動與我的內容重疊

我打算在這個項目中使用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包含一個空的框架佈局,使我可以添加使用的交易片段。我的片段內容出現,但頂部被工具欄隱藏。

+0

你使用任何看法尋呼機? –

回答

1

儘量安排,使得「content_main.xml」(其中有你的片段 容器)是「包括 -ed」,在「app_bar_main.xml」。

代碼:

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

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

content_main.xml

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

<!-- Created for MainActivity --> 
<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:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context=".MainActivity" 
    tools:showIn="@layout/app_bar_main"> 

    <!-- 
     Now, just attach your "fragment"(s) to above view with "id = fragment_container" 
    --> 

</LinearLayout> 
+0

超級!這工作...謝謝! –

+0

沒問題的人! –

1

包括像這樣的app_bar_main.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="bd.tahsincom.smartsell.MainActivity" 
    tools:showIn="@layout/activity_main2"> 

    <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="@color/colorPrimaryDark" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

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

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

在這裏一個新的佈局編寫代碼ü要在打開的應用程序默認的顯示設置和其能見度=「可見」

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);你在哪裏得到選定片段的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; 
      } 
     }