从标题xml更改文本

时间:2015-11-01 19:53:41

标签: android xml android-layout

我有一项活动,在加载时我想更新文本字段。该文本字段存储在外部XML文件中该活动布局的标题中。当尝试设置指向该XML文件的文本时,我得到nullpointer例外。

以下是我正在使用的活动:

public class NavDrawerMain extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

//JAC - need to add big search box right at the top of the drawer

//stuff for the user Timeline
private CustomAdapter timelineAdapter;
private ListView timelineList;

private PullToRefreshView mPullToRefreshView;

public static final int REFRESH_DELAY = 2000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nav_drawer_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    TextView userName = (TextView) findViewById(R.id.drawerUserName);
    userName.setText("test");

以下是该活动的布局:

<RelativeLayout
    android:layout_width="match_parent"
    android:background="#e5e5e5"
    android:layout_height="wrap_content">

    <com.yalantis.phoenix.PullToRefreshView
        android:id="@+id/pull_to_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/timeListView"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:divider="@null"
            android:dividerHeight="0dp"/>

    </com.yalantis.phoenix.PullToRefreshView>


</RelativeLayout>

<include layout="@layout/app_bar_nav_drawer_main"  android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView android:id="@+id/nav_view"
    android:layout_width="wrap_content" android:layout_height="match_parent"
    android:layout_gravity="start" android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_nav_drawer_main"
    app:menu="@menu/activity_nav_drawer_main_drawer" />

最后这里是外部xml文件,其中包含我想要更改的TextView。此文件是活动的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="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
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" android:orientation="vertical"
android:gravity="bottom">
android:id="+id/RelativeLayout1"      

<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" android:id="@+id/imageView" />

<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:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="android.studio@android.com" android:id="@+id/drawerUserName" />

textviews标识为drawerUserName。我曾经读过某个地方,我需要对布局进行充气,但是还没有能够解决这个问题。

1 个答案:

答案 0 :(得分:0)

在您的活动中,像这样充气header_layout

View headerView = LayoutInflater.from(this).inflate(R.layout.header_layout, null);
TextView userName = (TextView) headerView.findViewById(R.id.drawerUserName);
userName.setText("test");

现在将它作为HeaderView添加到NavigationView

navigationView.addHeaderView(headerView);

那应该是它!

对于熟悉的问题检查; NavigationView how to handle dynamic header content