2015-05-17 23 views
6

Tôi đang cố gắng tìm hiểu cách triển khai Ngăn điều hướng trong Android.Thanh thông báo có màu xám sau khi triển khai Nav Drawer

Trong một hoạt động, tôi đã thực hiện các Navigation Drawer đến dưới thanh Status Bar (trong suốt) và hơn App Bar và tất cả mọi thứ hoạt động tốt. (Ảnh chụp màn hình bên trái)

Trong một hoạt động trong cùng một ứng dụng, tôi cố gắng tạo Ngăn điều hướng kéo lên dưới Thanh ứng dụng. Nhưng ở đây, thanh trạng thái chuyển thành màu xám vì một lý do nào đó (cho dù ngăn điều hướng mở hay đóng) (Right Screenshot). Khác hơn thế này, mọi thứ có vẻ ổn.

Dưới đây là ảnh chụp màn hình:

enter image description here

Các xanh Nav Drawer là một mảnh.

Điều tôi muốn biết là làm cách nào để làm cho trạng thái bình thường (bóng tối hơn. Hãy nhớ, nếu tôi nhấp vào biểu tượng mũi tên kép đó trong Thanh ứng dụng, nó sẽ đưa tôi đến một hoạt động khác có chứa Ngăn kéo điều hướng khác . giống như trong thiết kế Vật liệu (Full chiều cao của màn hình và đi kèm theo một thanh Status Bar trong suốt) này được hiển thị ở phía bên trái của màn hình

dưới đây là các mã:.

MainActivity.java:

public class MainActivity extends ActionBarActivity { 
    Toolbar toolbar; 
    DrawerLayout xDrawerLayout; 

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

     toolbar = (Toolbar) findViewById(R.id.app_bar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 

     xDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     xDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.primary_dark)); 

     NavDrawerFragment mfragment = (NavDrawerFragment) getFragmentManager().findFragmentById(R.id.nav_drawer_fragment); 
     mfragment.SetUp(xDrawerLayout, toolbar, R.id.nav_drawer_fragment); 
    } 
... 

activity_alt.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:orientation="vertical"> 

    <include 
     android:id="@+id/app_bar" 
     layout="@layout/app_bar" /> 


    <android.support.v4.widget.DrawerLayout 
     android:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

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

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/hello_world" /> 

     </RelativeLayout> 

     <fragment 
      android:id="@+id/nav_drawer_fragment" 
      android:name="com.rt.droid.mattest.NavDrawerFragment" 
      android:layout_width="@dimen/nav_drawer_width" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      tools:layout="@layout/fragment_nav_drawer" /> 


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

</LinearLayout> 

NavDrawerFragment.java:

public class NavDrawerFragment extends Fragment { 

    private ActionBarDrawerToggle mDrawerToggle; 
    private DrawerLayout mDrawerLayout; 
    public DrawerLearner yDrawerLearner; 

    public NavDrawerFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View fView = inflater.inflate(R.layout.fragment_nav_drawer, container, false); 
     //fView.setFitsSystemWindows(true); 
     fView.setBackgroundColor(getResources().getColor(R.color.accent)); 
     return fView; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    public void SetUp(DrawerLayout drawerLayout, Toolbar toolbar, int frag_id) { 
     this.mDrawerLayout = drawerLayout; 
     View drawerFrag = getActivity().findViewById(frag_id); 
     mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) { 
      @Override 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       getActivity().invalidateOptionsMenu(); 
      } 

      @Override 
      public void onDrawerClosed(View drawerView) { 
       super.onDrawerClosed(drawerView); 
       getActivity().invalidateOptionsMenu(); 
      } 
     }; 

     yDrawerLearner = new DrawerLearner(mDrawerLayout, drawerFrag, getActivity()); 
     yDrawerLearner.execute(); 

     this.mDrawerLayout.setDrawerListener(mDrawerToggle); 
     mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.primary_dark)); 

     mDrawerLayout.post(new Runnable() { 
      @Override 
      public void run() { 
       mDrawerToggle.syncState(); 
      } 
     }); 

    } 
} 

fragment_nav_drawer.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.rt.droid.mattest.NavDrawerFragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_blank_fragment" /> 

</FrameLayout> 

app_bar.xml:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/primary" 
    app:popupTheme="@style/AppTheme.PopupMenu" 
    android:theme="@style/AppTheme.Toolbar"> 

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

styles.xml:

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="AppTheme.Base"> 
     <!-- Customize your theme here. --> 
    </style> 

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar"> 
     <item name="colorPrimary">@color/primary</item> 
     <item name="colorPrimaryDark">@color/primary_dark</item> 
     <item name="colorAccent">@color/accent</item> 
    </style> 

    <style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark"> 
     <item name="android:textColorPrimary">@color/primary_text</item> 
     <item name="android:textColorSecondary">@color/accent</item> 
    </style> 

    <style name="AppTheme.PopupMenu" parent="ThemeOverlay.AppCompat.Dark"> 
     <item name="android:background">@color/accent</item> 
    </style> 

</resources> 

styles.xml (v21):

<resources> 
    <style name="AppTheme" parent="AppTheme.Base"> 
     <!-- Customize your theme here. --> 
     <item name="android:colorPrimary">@color/primary</item> 
     <item name="android:colorPrimaryDark">@color/primary_dark</item> 
     <item name="android:colorAccent">@color/accent</item> 
     <item name="android:windowDrawsSystemBarBackgrounds">true</item> 
     <item name="android:statusBarColor">@android:color/transparent</item> 
     <item name="android:windowTranslucentStatus">true</item> 
    </style> 

</resources> 

Colors.xml (ảnh chụp màn hình cho thấy mẫu màu):

colors.xml

Lưu ý: Tôi không muốn thỏa hiệp về các kiểu vv sẽ khiến ngăn điều hướng khác của tôi không hoạt động. Nói cách khác, tôi thích một giải pháp trong đó cả hai loại thanh điều hướng hoạt động như mong đợi trong cùng một ứng dụng.

Vui lòng cho tôi biết nếu bạn cần bất kỳ thông tin nào và tôi sẽ chỉnh sửa nếu được yêu cầu. Chỉnh sửa: đã thêm app_bar.xml & colors.xml để làm rõ.

+0

loại bỏ điều này : DrawerLayout.setStatusBarBackgroundColor (getResources(). GetColor (R.color.primary_dark)); Đã xóa –

+0

nhưng thanh trạng thái vẫn có màu xám. – rapidclock

+0

sau đó vấn đề nằm trong thanh công cụ –

Trả lời

12

Nhưng ở đây, thanh trạng thái chuyển sang màu xám đối với một số lý do

Đó là mờ (25% màu đen) trên thanh trạng thái nền trên nền trắng của hoạt động bên dưới nó.

gì tôi muốn biết là làm thế nào để làm cho tình trạng bình thường

Bạn cần phải vô hiệu hóa thanh trạng thái mờ đối với hoạt động thứ hai. Nó được kích hoạt bởi dòng này trong định nghĩa chủ đề của bạn:

<item name="android:windowTranslucentStatus">true</item> 
<item name="android:statusBarColor">@color/primary_dark</item> 

Vì vậy, một trong hai định nghĩa một child theme và ghi đè lên lá cờ với false hoặc xóa cờ lập trình bằng cách gọi này từ hoạt động của bạn:

if (Build.VERSION.SDK_INT >= 21) { 
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 
    getWindow().setStatusBarColor(getResources().getColor(R.color.primary_dark); 
} 
+0

sau câu trả lời của bạn, tôi đã thử nghiệm với những gì bạn nói .. bạn đúng về TranslucentStatus và tôi lập trình thiết lập nó như bạn đã nói. Bây giờ thanh trạng thái là màu trắng (bây giờ nó chỉ trong suốt). Làm thế nào để tôi tắt tính minh bạch và đặt nó thành màu primary_dark (lập trình)? – rapidclock

+0

@rapidclock Rất tệ, hãy kiểm tra ngay bây giờ. –

0

add style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="android:windowDrawsSystemBarBackgrounds">true</item> 
    <item name="android:statusBarColor">@android:color/transparent</item> 
</style> 
+0

hmm, không nghĩ rằng đó là nó .. dòng đầu tiên là bình luận anyway. Dòng thứ hai chỉ cung cấp màu xanh cho đoạn (ví dụ: Ngăn kéo). – rapidclock

+0

Xin lỗi .. Vui lòng kiểm tra ngay bây giờ .. –

+0

Đã thử .. không thay đổi một điều đáng buồn. Trên thực tế dòng đó là tôi đang cố gắng khắc phục vấn đề bằng cách đưa màu tối cho thanh trạng thái, nhưng bằng cách nào đó không tạo ra sự khác biệt. – rapidclock

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