【问题标题】:Action Bar Doesn't work correctly操作栏无法正常工作
【发布时间】:2019-01-11 17:09:49
【问题描述】:

几年后我重新开始开发,所以有很多变化。 现在我正在尝试为活动修改 AppBar(工具栏)。 (我也看到了 CoordinatorLayout,但我不知道线性和相对有什么区别)。 所以在 MainActivity.class 我有:

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>

而且它工作正常,颜色和标题(或看起来)。

然后我还创建了另外两个活动(我从一个按钮打开(我将解释一个......他们提出了类似的问题): SecondActivity.class:

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);

    mTitle.setText(toolbar.getTitle());
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    mTitle.setTextColor(Color.parseColor("#ff0000"));
    toolbar.setBackgroundColor(Color.parseColor("#fafad2"));

}

和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),它们从顶部的应用程序开始,在工具栏上方,为什么?

非常感谢您的帮助。

【问题讨论】:

  • 提供您在第二个活动中更改文本的代码
  • 我的回答成功了吗?

标签: java android android-activity toolbar appbar


【解决方案1】:

您的问题是您将toolbarmappa_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。您可以删除 TextView 并调用 toolbar.setTitle("insert text here");,这样就不需要 TextView。

【讨论】:

  • 好吧,我试过了,它的标题文本有效,但如果我尝试 toolbar.setTitletextColor(Color.parseColor(""));,它不会改变
  • 如果去掉TextView,就不会有文字解释了
  • 您需要输入颜色。例如,黑色是 Color.parseColor("#000000"),白色是 Color.parseColor("#FFFFFF")
  • 您可以谷歌“十六进制颜色选择器”并选择一种颜色并使用前面带有#的6字符代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
  • 2013-02-01
相关资源
最近更新 更多