2016-05-26 31 views
9

Tôi chỉ mới bắt đầu học android, tôi đang học cách thêm AppBar. Nhưng nếu tôi sử dụng Bố cục tương đối làm gốc thì Thanh trạng thái không sử dụng màu "colorPrimaryDark" được xác định trong chủ đề. Nhưng thay đổi bố cục thành CoordinatorLayout làm cho nó hoạt động.Thanh trạng thái Màu không thay đổi với Bố cục tương đối làm phần tử gốc

Ai đó có thể giải thích sự khác biệt không? nó là một yêu cầu để sử dụng Layout CoordinatorLayout?

Hoạt động mở rộng AppCompatActivity.

<?xml version="1.0" encoding="utf-8"?> 
<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:fitsSystemWindows="true" 
    tools:context="com.example.ankit.designdemo.MainActivity"> 


    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/colorPrimary" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark"> 

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

enter image description here

<?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="com.example.ankit.designdemo.MainActivity"> 


    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/colorPrimary" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark"> 

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

enter image description here

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


public class MainActivity extends AppCompatActivity { 

    private DrawerLayout mDrawerLayout; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.custom_activity); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setHomeAsUpIndicator(R.drawable.ic_menu); 
     actionBar.setDisplayHomeAsUpEnabled(true); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 


} 
+0

bạn có thể chỉ cho phong cách nơi bạn định nghĩa 'colorPrimaryDark'? – hrskrs

+0

@hrskrs Đã thêm vào câu hỏi .. – Ankit

+0

Bạn có chắc chắn trang kiểu của bạn nằm trên 'values-v21' không? – hrskrs

Trả lời

19

Trong v21/styles.xml bạn có thể có dòng mã này:

<item name="android:statusBarColor">@android:color/transparent</item> 

Và như bạn đang chạy trên API 23, phong cách tuyên bố đang overrided từ những cái trong v21/styles.xml.

Là một giải pháp:

Chỉ cần loại bỏ dòng đó và nó sẽ làm việc tốt

+0

tại chỗ, vấn đề của tôi được giải quyết và như tôi đang học, bạn có thể vui lòng thêm làm rõ về ghi đè .. Tôi đã được ấn tượng rằng v21/styles.xml sẽ đến để chỉ sử dụng trên API 23 .. Cũng tại sao mọi thứ đã làm việc với CoordinatorLayout đó sẽ là một bổ sung tốt cũng – Ankit

+0

@Ankit Về phong cách bạn có thể đọc [ở đây] (https : //developer.android.com/guide/topics/ui/themes.html) về cách chúng hoạt động dựa trên nền tảng. Về 'CoordinatorLayout': nó hoạt động vì' android: fitsSystemWindows = "true" ', nếu bạn xóa nó, nó sẽ cho bạn thấy hành vi tương tự. – hrskrs

+1

Tôi thấy bây giờ .. vì vậy v21/styles.xml sẽ chạy khi API 21 trở lên .. Cảm ơn bạn đã trợ giúp .. – Ankit

0

Kiểu dáng của statusbar được thực hiện bằng thẻ

android:fitsSystemWindows 

. Điều này hiện không được hỗ trợ cho RelativeLayout. Hoặc bạn thử bố cục khác như FrameLayout có thể hoạt động nhưng để biết tôi sẽ đề xuất gắn với CoordinatorLayout.

Có lẽ bạn sẽ có được một số chi tiết ở đây: https://medium.com/google-developers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec

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