2017-02-27 21 views
1

當我在低於棉花糖設備的設備上運行我的應用程序時,它只顯示一個標題欄。但是,當我在棉花糖設備嘗試它時,導航抽屜出現在第二個標題欄中。像這樣。爲什麼在我的活動中有2個標題欄在棉花糖設備中使用導航抽屜

sadasasdsad

有人能告訴我如何解決這一問題?

我的MainActivity,activity_main,清單和樣式如下。

謝謝。

MainActivity

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


try { 


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    setTitle("Case Sheets"); 

    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); 

    myDB = MainActivity.this.openOrCreateDatabase("hoteldb", MODE_PRIVATE, null); 

     mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
     RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
     mRecyclerView.setLayoutManager(mLayoutManager); 
     mRecyclerView.setItemAnimator(new DefaultItemAnimator()); 
     mRecyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext())); 
     try { 

      showList(); 

     }catch (Exception e) 
     { 
      Toast.makeText(getApplicationContext(),"Please Synch the data",Toast.LENGTH_SHORT).show(); 
     } 



    } catch (Exception e) { 
     e.printStackTrace(); 
     Toast.makeText(getApplicationContext(),"Please Login",Toast.LENGTH_LONG).show(); 
     Intent logout = new Intent(MainActivity.this, LoginPage.class); 
     startActivity(logout); 

    } 



} 

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

<android.support.design.widget.CoordinatorLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" 
     android:layout_width="match_parent" 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.CoordinatorLayout> 

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

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.admin.digitalmenu" > 

<uses-sdk 
    android:minSdkVersion="7" 
    android:targetSdkVersion="15" /> 
<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
<uses-permission android:name="android.permission.BLUETOOTH"/> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 


<application 
    android:icon="@drawable/logo" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme"> 
    <!-- android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" > --> 
    <activity 
     android:name=".Oodo.Login.LoginPage" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" 
     > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".Oodo.CaseSheets.MainActivity" 
     android:theme="@style/AppTheme.NoActionBar" 
     android:screenOrientation="portrait"></activity> 

    </application> 

style.xml

<resources> 

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 

    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 
<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 




<style name="heading_text"> 
    <item name="android:textColor">#ff000000</item> 
    <item name="android:textStyle">bold</item> 
    <item name="android:textSize">16sp</item> 
    <item name="android:padding">5dp</item> 
</style> 
<style name="HomeButton"> 
    <item name="android:layout_gravity">center_vertical</item> 
    <item name="android:layout_width">fill_parent</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:layout_weight">1</item> 
    <item name="android:gravity">center_horizontal</item> 
    <item name="android:textSize">@dimen/text_size_medium</item> 
    <item name="android:textStyle">normal</item> 
    <item name="android:textColor">@color/colorPrimary</item> 
    <item name="android:background">@null</item> 
</style> 

</resources> 

回答

1

我終於修好了。

我所做的是:

1.Deleted從佈局

2.Changed我MainActivity這樣的工具欄。 refer here

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 


try { 


Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
setTitle("Case Sheets"); 

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
mDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) { 
     @Override 
     public void onDrawerClosed(View drawerView) { 
      super.onDrawerClosed(drawerView); 
     } 

     @Override 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
     } 
    }; 
    drawer.post(new Runnable() { 
     @Override 
     public void run() { 
      mDrawerToggle.syncState(); 
     } 
    }); 
    drawer.setDrawerListener(mDrawerToggle); 
    mDrawerToggle.syncState(); 

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

myDB = MainActivity.this.openOrCreateDatabase("hoteldb", MODE_PRIVATE, null); 

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
    mRecyclerView.setLayoutManager(mLayoutManager); 
    mRecyclerView.setItemAnimator(new DefaultItemAnimator()); 
    mRecyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext())); 
    try { 

     showList(); 

    }catch (Exception e) 
    { 
     Toast.makeText(getApplicationContext(),"Please Synch the data",Toast.LENGTH_SHORT).show(); 
    } 



} catch (Exception e) { 
    e.printStackTrace(); 
    Toast.makeText(getApplicationContext(),"Please Login",Toast.LENGTH_LONG).show(); 
    Intent logout = new Intent(MainActivity.this, LoginPage.class); 
    startActivity(logout); 

} 



} 

,並保持該活動的主題爲Apptheme本身。

它工作在兩個MARSHMELLOW,棒棒糖設備..

@ rafsanahmad007..Thankü幫助。

0

工具欄初始化後添加此setSupportActionBar(myToolbar);

1

你缺少這樣的:setSupportActionBar()

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
setSupportActionBar(toolbar); 
價值觀

/styles.xml:

<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 

應用主題:烏拉圭回合活動清單

在清單:

<activity 
    android:name=".Oodo.CaseSheets.MainActivity" 
    android:theme="@style/AppTheme.NoActionBar" //change here 
+0

'java.lang.IllegalStateException:此活動已經有一個由窗口裝飾提供的操作欄。請勿在您的主題中請求Window.FEATURE_SUPPORT_ACTION_BAR並將windowActionBar設置爲false以代替使用工具欄。' – Ben

+0

出現此錯誤。 – Ben

+0

我必須刪除這兩行才能正常工作。 getSupportActionBar()。setDisplayHomeAsUpEnabled(真); setSupportActionBar(toolbar); 現在我不能在marshmellow下面的d​​evies上運行它時看到任何標題欄。 – Ben