setTitleTextColor不会更改文本的颜色

时间:2018-08-13 13:36:04

标签: android android-toolbar

我是Android编程的新手,正在尝试将白色添加到我使用setTitleTextColor()方法以编程方式添加的工具栏的文本中,但是颜色仍然为黑色。怎么做?

这是我的代码

package com.example.android.tourguide;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


public class DetailClassForListItemOption extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail_layout);
    final Intent intent = getIntent();
    TextView informationTextView = findViewById(R.id.information_about_the_place);
    informationTextView.setText(intent.getStringExtra("informationAboutPlace"));
    TextView timingsTextView = findViewById(R.id.timings_of_the_venue);
    timingsTextView.setText(intent.getStringExtra("timingsOfThePlace"));
    android.support.v7.widget.Toolbar nameOfTheToolbar = findViewById(R.id.toolbar_for_detail_layout);
    nameOfTheToolbar.setTitle(intent.getStringExtra("nameForToolbar"));
    nameOfTheToolbar.setTitleTextColor(Color.WHITE);
    setSupportActionBar(nameOfTheToolbar);

    ImageView imageInsideThePlace = findViewById(R.id.image_of_the_inside_of_a_place);
    imageInsideThePlace.setImageResource(intent.getIntExtra("imageOfTheInsideOfThePlace", 0));
    FloatingActionButton locationOfVenueFab = findViewById(R.id.floating_action_button);
    locationOfVenueFab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Uri gmmIntentUri = Uri.parse(intent.getStringExtra("coordinatesOfThePlace"));
            Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
            mapIntent.setPackage("com.google.android.apps.maps");
            mapIntent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
            if (mapIntent.resolveActivity(getPackageManager()) != null) {
                startActivity(mapIntent);
            }
        }
    });
}
}

编辑:这里在上面使用的活动的xml

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nested_scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.FloatingActionButton
                        android:id="@+id/floating_action_button"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentTop="true"
                        android:clickable="true"
                        android:src="@drawable/baseline_location_on_white_24" />

                    <android.support.v7.widget.CardView
                        android:id="@+id/general_information_card_view"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="8dp">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical">

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_margin="8dp"
                                android:text="@string/bengaluru_general_information"
                                android:textColor="@android:color/black"
                                android:textSize="24sp"
                                android:textStyle="bold" />

                            <TextView
                                android:id="@+id/information_about_the_place"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginBottom="8dp"
                                android:layout_marginLeft="8dp"
                                android:layout_marginRight="8dp"
                                android:textStyle="italic" />
                        </LinearLayout>
                    </android.support.v7.widget.CardView>
                </RelativeLayout>

                <android.support.v7.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="8dp"
                            android:layout_marginLeft="8dp"
                            android:layout_marginRight="8dp"
                            android:text="@string/timings"
                            android:textColor="@android:color/black"
                            android:textSize="24sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/timings_of_the_venue"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="8dp"
                            android:layout_marginLeft="8dp"
                            android:layout_marginRight="8dp"
                            android:textSize="18sp"
                            android:textStyle="italic|bold" />
                    </LinearLayout>
                </android.support.v7.widget.CardView>
            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>

        <android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="256dp">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:contentScrim="@color/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:id="@+id/image_of_the_inside_of_a_place"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax"
                    tools:ignore="ContentDescription" />

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar_for_detail_layout"
                    android:layout_width="match_parent"
                    android:layout_height="52dp"
                    android:elevation="6dp"
                    app:layout_collapseMode="pin" />
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
    </android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>

这是应用程序的屏幕截图 enter image description here

1 个答案:

答案 0 :(得分:0)

您有一个折叠工具栏,因此必须为此设置颜色:

CollapsingToolbarLayout ct;
ct = findViewById(R.id.collapsing_toolbar_layout);
ct.setTitle(title);
ct.setCollapsedTitleTextColor(Color.WHITE);