8

tôi muốn làm navigation bar mờ của tôi, vì vậy tôi đã làm một số điều như thế này:vấn đề với windowTranslucentNavigation

<style name="BlueToolBar" parent="Theme.AppCompat"> 
    <item name="windowActionBar">false</item> 
    <item name="android:colorBackground">@color/action_blue</item> 
    <item name="colorPrimary">@color/action_blue</item> 
    <item name="colorPrimaryDark">@color/status_bar</item> 
    <item name="android:windowTranslucentNavigation">true</item> 
</style> 

và nó đang làm việc độc đáo, nhưng thanh công cụ của tôi và thanh trạng thái được sáp nhập, merged toolbar

khi tôi sử dụng này:

<style name="BlueToolBar" parent="Theme.AppCompat"> 
    <item name="windowActionBar">false</item> 
    <item name="android:colorBackground">@color/action_blue</item> 
    <item name="colorPrimary">@color/action_blue</item> 
    <item name="colorPrimaryDark">@color/status_bar</item> 
</style> 

các resualt là:

nice toolbar

Trả lời

7

<item name="android:windowTranslucentNavigation">true</item> không chỉ làm cho thanh điều hướng trong suốt, nó còn khiến hoạt động được vẽ phía sau thanh trạng thái.

Để giải quyết vấn đề này, bạn có thể đặt lề ở trên cùng của thanh công cụ.

Bắt chiều cao của Statusbar có thể được thực hiện thông qua:

public static int getStatusBarHeight(Context context) { 
    int result = 0; 
    int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); 
    if (resourceId > 0) { 
     result = context.getResources().getDimensionPixelSize(resourceId); 
    } 
    return result; 
} 

Bây giờ thiết lập lề vào thanh công cụ qua này (nếu ToolBar bạn đang ở trong một LinearLayout):

((LinearLayout.LayoutParams) toolbar.getLayoutParams()).setMargins(0, getStatusBarHeight(this), 0, 0); 
11

có giải pháp tốt hơn:

chỉ cần đặt android:fitsSystemWindows="true" thành xml bố cục mẹ của bạn. hệ thống sẽ cẩn thận thiết lập thích hợp paddingTop