2
如您所知,android studio擁有一個導航抽屜活動,它在佈局文件夾內創建了一堆佈局。如何動態更改導航抽屜的頁眉佈局背景顏色?
nav_header_main.xml
是包含NavigationView頭的部件的佈局。我在上面的圖片強調它,它具有下面的代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sideNavLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:src="@android:drawable/sym_def_app_icon" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[email protected]" />
</LinearLayout>
我想從MainActivity.java
在上面的圖片來改變突出部分的背景下,像這樣:
LinearLayout sideNavLayout;
sideNavLayout = (LinearLayout) findViewById(R.id.sideNavLayout);
sideNavLayout.setBackgroundResource(R.drawable.my_side_nav_bar);
但在哪裏我使用它我得到的錯誤:
java.lang.NullPointerException
任何人都可以在這方面幫助我嗎?
非常感謝。我很感激幫助。不幸的是,我得到兩個頭一個在另一個之上。您的第三行中的變量視圖也不會被使用。 – Darush
如何刪除現在位於所需標題下的第一個標題? – Darush
但它們都是在單個navview中實現的嗎?所以你應該這樣。並糾正它第三個參數刪除。我編輯了我的答案 – uguboz