How to set text from bundle (toolbar) 我不知道怎么设置。
工具栏.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ToolbarColor">
<TextView
android:id="@+id/textView_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="My example"
android:gravity="center"/>
</android.support.v7.widget.Toolbar>
DetailActivity.java
Bundle bundle = getIntent().getExtras();
if (bundle!=null){
String name = bundle.getString("nameCar");
}
Toolbar toolbar = (Toolbar) findViewById(R.id.m_tool_bar_detail);
TextView textToolbar = (TextView) toolbar.findViewById(R.id.textView_toolbar);
setSupportActionBar(toolbar);
if(getSupportActionBar()!=null){
textToolbar.setText(name); ///ERROR(name)
getSupportActionBar().setIcon(getDrawable(R.drawable.ic_action_name));
}
我试过 textToolbar.setText("xxxxx");
但它给出了一个错误。
我想在包中获取 name
以在工具栏中设置 Textview
。
最佳答案
I want to get name in bundle to set Textview in toolbar
您应该将 String
名称设置为全局。
String name = null;
然后
if (bundle!=null){
name = bundle.getString("nameCar");
}
关于java - 如何从包(工具栏)设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44426402/