Ich bin neu in Android-Entwicklung, ich habe versucht, um Google und Stackoverflow suchen, aber keine Fragen oder Lösungen gefunden.Wie ändert man NavigationView Header Hintergrund programmatisch?
Ich möchte NavigationView Header-Hintergrund programmgesteuert ändern, aber etwas, das ich nicht will.
dies MainActivity i versuchen Navigation Header Hintergrund in dieser Art und Weise zu ändern:
View header = navigationView.getHeaderView(0);
tv_nav_header = (TextView)header.findViewById(R.id.text_nav_header);
tv_nav_header.setText(username);
tv_nav_header.setBackground(drawable);
dies mein nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="160dp">
<cn.xyz.util.CircleImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="40dp"
android:layout_marginStart="10dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/text_nav_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:layout_marginStart="70dp"
android:text="tempLocalUser"
android:textColor="#FFFFFF"
android:background="@android:color/transparent"/>
</RelativeLayout>
activity_main.xml ist
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="100" />
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_drawer" />
</android.support.v4.widget.DrawerLayout>
die tatsächliche Zeigen Sie so, das ist nicht mein Wunsch enter image description here
und unten ist mein wollen, ist dies Umset von nav_header.xml RelativeLayout attr
android: background = "@ color/colorPrimary_blue"
Sorry für mein schlechtes Englisch.
Ja, du hast recht, danke – feiffy
freundlicher upvote und die Antwort dann akzeptieren :) – jaibatrik