Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何以编程方式更改NavigationView标题背景?_Android_Android Navigationview - Fatal编程技术网

Android 如何以编程方式更改NavigationView标题背景?

Android 如何以编程方式更改NavigationView标题背景?,android,android-navigationview,Android,Android Navigationview,我想以编程方式更改NavigationView标题背景,但得到了我不想要的东西 这是我尝试用以下方式更改导航标题背景的主要活动: View header = navigationView.getHeaderView(0); tv_nav_header = (TextView)header.findViewById(R.id.text_nav_header); tv_nav_header.setText(username);

我想以编程方式更改NavigationView标题背景,但得到了我不想要的东西

这是我尝试用以下方式更改导航标题背景的主要活动:

    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);
这是我的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

    <?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>

下面是我的需求,这是由nav_header.xml RelativeLayout attr实现的

android:background=“@color/colorPrimary\u blue”


您正在为文本视图设置背景色。相反,请尝试在RelativeLayout上设置颜色。试试下面的方法-

View header = navigationView.getHeaderView(0);
header.setBackground(drawable);
tv_nav_header = (TextView)header.findViewById(R.id.text_nav_header);
tv_nav_header.setText(username);
tv_nav_header.setBackground(drawable);