尽管设置属性,Android工具栏标题,副标题和后退按钮仍未显示

时间:2017-02-13 21:23:46

标签: java android xml android-support-library android-toolbar

创建工具栏以替换操作栏后,工具栏未生成预期结果。工具栏本身会根据需要显示指定的颜色,但由于某种原因,标题,副标题和后退按钮根本不显示。活动的主题甚至已设置为Theme.AppCompat.NoActionBar

布局/ toolbar_turquoise.xml

<?xml version="1.0" encoding="utf-8"?>
<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/toolbar_turquoise"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/turquoise"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

TurquoiseActivity.java

public class TurquoiseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null) {
            Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_turquoise);
            setSupportActionBar(myToolbar);

            myToolbar.setTitle("Hello World");
            myToolbar.setTitleTextColor(Color.parseColor("#000099"));
            myToolbar.setSubtitle("Lorem ipsum dolor sit amet");
            myToolbar.setSubtitleTextColor(Color.parseColor("#000099"));

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material);
            upArrow.setColorFilter(Color.parseColor("#000099"), PorterDuff.Mode.SRC_ATOP);
            getSupportActionBar().setHomeAsUpIndicator(upArrow);
        }

        FragmentTurquoise newFragment = new FragmentTurquoise();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.master_container, newFragment);
        transaction.commit();
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个,

XML文件:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/turquoise"
            android:minHeight="100dp">

        </android.support.v7.widget.Toolbar>

    </RelativeLayout>

Java类:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if(toolbar != null) {
        setSupportActionBar(toolbar);

        toolbar.setTitle("Hello World");
        toolbar.setTitleTextColor(Color.parseColor("#000099"));
        toolbar.setSubtitle("Lorem ipsum dolor sit amet");
        toolbar.setSubtitleTextColor(Color.parseColor("#000099"));
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material);
        upArrow.setColorFilter(Color.parseColor("#000099"), PorterDuff.Mode.SRC_ATOP);
        getSupportActionBar().setHomeAsUpIndicator(upArrow);
    }