为什么我的工具栏没有以编程方式更改颜色?

时间:2019-12-27 08:58:01

标签: java android

我有这段代码,可以通过编程方式更改工具栏的颜色,但只有状态栏可以更改其颜色,而不能更改工具栏本身。

@Override
public void setupToolbar(String title) {

    toolbarShowWithBack(title);
    Window window = this.getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.setStatusBarColor(ContextCompat.getColor(this, R.color.highlightGreen));
}

预期的输出应如下所示: Expected output 但是我的代码显示了这一点: Current output of my code

这是我的xml布局。我尚未添加工具栏,因为我想使用默认工具栏。

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="@color/whitesmoke"
    tools:context="com.sample.sample.ui.mart.activity.viewreceipt.ViewReceiptActivity">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/main_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/whitesmoke">


            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_view_receipt_shopname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:itemCount="1"
                tools:listitem="@layout/layout_view_receipt_shopname" />

            <com.metromart.metromart.ui.mart.customview.OrderBreakDownView
                android:id="@+id/order_break_down"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:visibility="visible"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/rv_view_receipt_shopname">

            </com.metromart.metromart.ui.mart.customview.OrderBreakDownView>

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_marginTop="16dp"
                app:layout_constraintTop_toBottomOf="@id/order_break_down">

                <View
                    android:id="@+id/view38"
                    android:layout_width="0dp"
                    android:layout_height="1dp"
                    android:background="@color/lightGrey"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/textView45"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp"
                    android:text="Charges"
                    android:textColor="@color/black"
                    android:textSize="20dp"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <View
                    android:id="@+id/view29"
                    android:layout_width="0dp"
                    android:layout_height="1dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginEnd="16dp"
                    android:background="@color/lightGrey"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/textView45" />

                <ImageView
                    android:id="@+id/iv_receipt_payment_method"
                    android:layout_width="30dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="8dp"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/view29"
                    app:srcCompat="@drawable/selector_payment_method" />

                <TextView
                    android:id="@+id/tv_receipt_payment_method"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:textColor="@color/black"
                    android:textSize="20dp"
                    app:layout_constraintBottom_toBottomOf="@+id/iv_receipt_payment_method"
                    app:layout_constraintStart_toEndOf="@+id/iv_receipt_payment_method"
                    app:layout_constraintTop_toTopOf="@+id/iv_receipt_payment_method"
                    tools:text="Paid with credit card" />

                <View
                    android:id="@+id/view30"
                    android:layout_width="0dp"
                    android:layout_height="1dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="16dp"
                    android:background="@color/lightGrey"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/iv_receipt_payment_method" />

                <TextView
                    android:id="@+id/textView47"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginBottom="16dp"
                    android:text="Final Charge"
                    android:textColor="@color/black"
                    android:textSize="20dp"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/view30" />

                <TextView
                    android:id="@+id/tv_receipt_final_charge"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="16dp"
                    android:textColor="@color/black"
                    android:textSize="20dp"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="@+id/textView47"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/textView47"
                    tools:text="P262.75" />

                <View
                    android:id="@+id/view37"
                    android:layout_width="0dp"
                    android:layout_height="1dp"
                    android:background="@color/lightGrey"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent" />

            </android.support.constraint.ConstraintLayout>

        </android.support.constraint.ConstraintLayout>

    </android.support.v4.widget.NestedScrollView>

</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案