Java 操作栏不使用';我不能正常工作
几年后我重新开始开发,所以有很多变化。 现在,我正在尝试修改活动的AppBar(工具栏)。(我也看到了坐标布局,但我不知道线性和相对布局有什么区别)。 因此,在MainActivity.class中,我有:Java 操作栏不使用';我不能正常工作,java,android,android-activity,toolbar,appbar,Java,Android,Android Activity,Toolbar,Appbar,几年后我重新开始开发,所以有很多变化。 现在,我正在尝试修改活动的AppBar(工具栏)。(我也看到了坐标布局,但我不知道线性和相对布局有什么区别)。 因此,在MainActivity.class中,我有: public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInst
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView mTitle = (TextView)
toolbar.findViewById(R.id.toolbar_title);
setSupportActionBar(toolbar);
mTitle.setText(toolbar.getTitle());
getSupportActionBar().setDisplayShowTitleEnabled(false);
mTitle.setTextColor(Color.parseColor("#ff0000"));
在activity_main.xml中:
<android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title"
android:layout_gravity="center"
android:id="@+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
和activity_second.xml:
<android.support.constraint.ConstraintLayout 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=".ActivityMappa">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarmappa"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_mappa"
android:layout_gravity="center"
android:id="@+id/toolbarmappa_title" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</android.support.constraint.ConstraintLayout>
在第二个活动中,工具栏的颜色是正确的,但是当我运行应用程序时,文本没有改变(在android studio预览中,它被改变),文本在@string/title_mappa中,并且它存在。
那么为什么文本没有改变呢?代码是一样的
还有一件事,当我在content_main.xml中添加内容时,位置从工具栏下方开始,如果我设置边距/填充,则从它开始,但从其他2个活动开始,当我添加其他内容(如imageview)时,它们从工具栏上方顶部的应用程序开始,为什么
非常感谢您的帮助。您的问题是将
toolbarmappa\u title
的文本设置为toolbarmappa
的标题,但是toolbarmappa
的标题为空,因此您将文本设置为空。在获取标题之前,需要使用预定义字符串设置文本或设置toolbarmappa
的标题
应该是这样的:
public class ActivityMappa extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mappa);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarmappa);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbarmappa_title);
setSupportActionBar(toolbar);
toolbar.setTitle("insert title here");
mTitle.setText(toolbar.getTitle());
getSupportActionBar().setDisplayShowTitleEnabled(false);
mTitle.setTextColor(Color.parseColor("#ff0000"));
toolbar.setBackgroundColor(Color.parseColor("#fafad2"));
}
此外,更改工具栏文本的一般做法不是添加文本视图。您可以删除TextView并调用
toolbar.setTitle(“在此处插入文本”)代码>这样就不需要文本视图。提供在第二个活动中更改文本的代码我的答案成功了吗?好的,我尝试了,对于标题文本,它可以工作,但是如果我尝试toolbar.setTitletextColor(Color.parseColor(“”);,它不会改变,如果删除文本视图,则不会解释任何文本。您需要输入颜色。例如,黑色是颜色。parseColor(#000000“)和白色是颜色。parseColor(#FFFFFF”)您可以在谷歌上搜索“十六进制颜色选择器”并选择一种颜色,然后使用前面带有#
的6个字符的代码。
public class ActivityMappa extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mappa);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarmappa);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbarmappa_title);
setSupportActionBar(toolbar);
toolbar.setTitle("insert title here");
mTitle.setText(toolbar.getTitle());
getSupportActionBar().setDisplayShowTitleEnabled(false);
mTitle.setTextColor(Color.parseColor("#ff0000"));
toolbar.setBackgroundColor(Color.parseColor("#fafad2"));
}