Android 在布局的文本视图中将文本动态设置为标题布局
我的导航视图中有一个主活动布局,它使用另一个布局作为标题布局。我想动态更改用作标题布局的文本视图文本。在我的主要活动中使用此选项:Android 在布局的文本视图中将文本动态设置为标题布局,android,textview,Android,Textview,我的导航视图中有一个主活动布局,它使用另一个布局作为标题布局。我想动态更改用作标题布局的文本视图文本。在我的主要活动中使用此选项: DrawerLayout drawerLayout; TextView textview; Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVie
DrawerLayout drawerLayout;
TextView textview;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
TextView t = (TextView)findViewById(R.id.name);
t.setText("Some String Here");
}
Main_activity.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/drawer_layout"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/toolbar_layout"
/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/navigation_view"
android:layout_gravity="start"
app:menu="@menu/drawer_menu"
app:headerLayout="@layout/navigation_drawer_header"
>
</android.support.design.widget.NavigationView>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="#a3b1b2"
>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/photo"
android:layout_margin="35dp"
android:id="@+id/img"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ismail Zakky"
android:layout_toRightOf="@+id/img"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="50dp"
android:id="@+id/name"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ismailzakky@yahoo.com"
android:layout_toRightOf="@+id/img"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_below="@+id/name"
android:id="@+id/email"
/></RelativeLayout>
你能帮我解决这个问题吗?
顺便说一句,我的英语很差。我希望你能明白这一点。在
Main\u activity.xml
中,提供了NavigationView
的ID(例如android:ID=“@+ID/navigation”
)
在onCreate()
中:
你可以试试这个
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
TextView t = (TextView)drawerLayout.findViewById(R.id.name);
我认为您必须从标题视图中找到R.id.name。TextView name=(TextView)header.findViewById(R.id.name)@贾普勒是的,很抱歉疏忽了=(它不在抽屉里。
NavigationView navigation = (NavigationView) findViewById(R.id.navigation);
View header = navigation.getHeaderView(0);
TextView name = (TextView) header.findViewById(R.id.name);
name.setText("Your name");
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
TextView t = (TextView)drawerLayout.findViewById(R.id.name);