2
我有活動,我在用戶觸摸事件上使用了五個片段。我想爲3個片段設置狀態欄顏色,其他兩個片段沒有狀態欄。如何更改片段的狀態欄顏色?
如果我在一個片段的onCreateView方法中使用下面的代碼,它會更改活動的狀態欄,並且我使用getActivity()方法獲得所有片段的白色。所以我正在尋找碎片的解決方案。
getActivity().getWindow.setStatusBatColor(Color.White);
我也嘗試在style.xml中設置我的主題,並將其應用於xml中的fragemnts根佈局,但它也不能正常工作。
styles.xml
<style name="StatusBarColor" parent="AppBaseTheme">
<item name="android:statusBarColor">#F5F5F5
</item>
</style>
fragment1.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="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:orientation="vertical"
android:theme="@style/StatusBarColor"
android:background="@color/home_bg_white"
tools:context="mycontext">
預先感謝您的幫助。
我想改變片段狀態欄,所以在片段類中它不允許我添加getWindow方法,那就是問題。 –
您的片段必須是活動的一部分。你可以用活動的方法編寫這段代碼,然後從你的片段中調用它。它會解決你的問題。 –
使用我在我的編輯部分 –