對於菜單欄的透明化在activity中的代碼是很少的。主要操作佈局文件和註冊清單文件
網上把菜單欄的透明化分了兩個版本,4.4以後,和5.0以後。下面我寫的是5.0以後的。(親測可用)
第一步:你的activity需要禁用掉標題欄。
方法:我是定義一個基類,裏面主要是 supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
}
}
第二步:在你的activity佈局文件中自定義一個標題欄(這個我就略寫了,直接用一個RelativeLayout佈局代替)
<RelativeLayout android:id="@+id/bbbbb" android:layout_width="match_parent" android:layout_height="55dp" android:background="#你自己定義的顏色"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="標題欄---" android:textColor="@android:color/white" android:textSize="20sp" /> </RelativeLayout>
第三步:在你的佈局文件根佈局中添加幾行代碼
1. android:fitsSystemWindows="true" 2. android:background="#你自己定義的顏色" //上文你標題欄定義的顏色
第四步:自定義一個主題,在res目錄下的value文件夾裏面的style.xml文件
<resources> <!-- 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="TransparentTheme" parent="AppTheme"></style>
</resources>
第五步:在配置清單文件中引用這個佈局,也就是在配置清單文件裏的 android:theme="@style/TransparentTheme"這個屬性。