2014-11-08 44 views
6

Tôi hiện đang cố gắng chuyển đổi ứng dụng của mình thành thiết kế material design và đã gặp phải một số vấn đề với thanh tác vụ/thanh công cụ. Thực hiện mọi thứ theo cách tôi đã làm, thanh tác vụ/thanh công cụ sẽ không hiển thị trên thiết bị lollipop hoặc kitkat. Có lẽ ai đó có thể nhìn vào chủ đề của tôi, phong cách, activity_main (nơi tôi giữ một loạt các mảnh và là nơi duy nhất tôi đặt mã xml thanh công cụ) và tính chủ động.ActionBar/Thanh công cụ không hiển thị trên phiên bản Lollipop của ứng dụng

Cảm ơn.

giá trị/styles.xml

<resources> 

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light"> 
    <!-- Set AppCompat’s actionBarStyle --> 
    <item name="actionBarStyle">@menu/action_menu</item> 

    <!-- The rest of your attributes --> 
</style> 

</resources> 

giá trị/themes.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<style name="AppTheme" parent="AppTheme.Base"/> 

<style name="AppTheme.Base" parent="Theme.AppCompat"> 
    <item name="colorPrimary">@color/material_blue_grey_800</item> 
    <item name="colorPrimaryDark">@color/material_blue_grey_950</item> 
    <item name="android:textColor">@color/black</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="windowActionBar">false</item> 
</style> 
</resources> 

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawerLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<RelativeLayout 
    android:id="@+id/drawerView" 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="start" > 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:minHeight="?attr/actionBarSize" /> 

    <ListView 
     android:id="@+id/drawerList" 
     android:layout_width="250dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="left" 
     android:background="@drawable/nav_border" 
     android:divider="@null" /> 
</RelativeLayout> 
<!-- 
     <fragment 
     android:id="@+id/fragment1" 
     android:name="com.shamu11.madlibsportable.MadlibsSelect" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

--> 

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

MainActivity

public class MainActivity extends ActionBarActivity implements 
    OnItemClickListener { 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

    Fragment fragment = new MadlibsSelect(); 

    fragment = new MadlibsSelect(); 

    FragmentTransaction fm = getSupportFragmentManager().beginTransaction(); 
    fragment.setArguments(getIntent().getExtras()); 
    fm.add(R.id.fragment_container, fragment); 
    fm.commit(); 

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

    toolbar.inflateMenu(R.menu.action_menu); 

    //ActionBar bar = getActionBar(); 
    //bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#428bca"))); 

    ...more stuff happen down here including adding nav drawer. 

Trả lời

4

Thay đổi bạn activity_main.xml như sau

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

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

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:minHeight="?attr/actionBarSize" /> 

     <FrameLayout 
      android:id="@+id/fragment_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/drawerView" 
     android:layout_width="250dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="start" > 

     <ListView 
      android:id="@+id/drawerList" 
      android:layout_width="250dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="left" 
      android:background="@drawable/nav_border" 
      android:divider="@null" /> 
    </RelativeLayout> 
    <!-- 
     <fragment 
     android:id="@+id/fragment1" 
     android:name="com.shamu11.madlibsportable.MadlibsSelect" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
    --> 

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

và Nếu bạn đang sử dụng setSupportActionBar sau đó toolbar.inflateMenu sẽ không hoạt động.

Để biết thêm chi tiết, hãy kiểm tra bài đăng này Android Usages Of Toolbar. Nó có thể giúp bạn.

+0

BANG ON! Cảm ơn! – sanic

+0

Loại câu trả lời tồi tệ nhất. Có phải những người còn lại phải làm một sự khác biệt trên hai tệp đó bây giờ không? –

4

Hoạt động của bạn phải mở rộng từ ActionBarActivity khi sử dụng thư viện AppCompat mới.

+0

Đã làm điều đó, đồng thời triển khai onitemclicklistener Tôi đã thêm mã đó để làm rõ. – sanic

+0

@ shamu11 Bạn đã thử thay đổi chủ đề gốc của bạn thành "Theme.AppCompat.Light.NoActionBar" Bạn đã khai báo thanh công cụ trong bố cục của mình, vì vậy bạn nên ổn. –

+1

Thực ra tôi vừa thay đổi true từ true thành false và bây giờ tôi có một thanh hành động! Bạn có thể giới thiệu một cách để đi một tinker với thanh hành động này và thêm chuyển đổi cho nó? Cảm ơn! – sanic

1

Có điều này xảy ra, tôi đã lãng phí hơn một giờ để sửa lỗi này.

@Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    // Sync the toggle state after onRestoreInstanceState has occurred. 
    mDrawerToggle.syncState(); 
    } 

Điều này sẽ tự động đồng bộ hóa và cuối cùng hiển thị biểu tượng quá mức.

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    // Pass any configuration change to the drawer toggls 
    mDrawerToggle.onConfigurationChanged(newConfig); 
} 

Điều đó phù hợp với tôi, Hy vọng điều đó sẽ hữu ích.

Các vấn đề liên quan