我有一个工具栏,可以在滚动时更改颜色,基本上颜色是透明的,而在滚动时更改颜色,我只想设置一种颜色而不是透明,而只显示一种颜色
我试图更改工具栏xml的颜色并在活动中显示scroling
这是我的toolbar.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"
xmlns:tools="http://schemas.android.com/tools"
style="@style/ToolBarStyle"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:visibility="visible"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp" />
此工具栏包含在该活动的另一个活动中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<include
android:id="@+id/toolbar_actionbar"
layout="@layout/toolbar_default"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar_actionbar">
</FrameLayout>
</RelativeLayout>
而activity.java是
package com.sherdle.universal.util;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.sherdle.universal.R;
import com.sherdle.universal.util.layout.TrackingScrollView;
public abstract class DetailActivity extends AppCompatActivity {
protected RelativeLayout coolblue;
protected Toolbar mToolbar;
protected ImageView thumb;
boolean FadeBar = true;
protected int mScrollableHeaderHeight;
protected int latestAlpha;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
protected void setUpHeader(String imageurl){
if (isTablet()){
FadeBar = false;
} else {
coolblue.setVisibility(View.GONE);
}
if ((null != imageurl && !imageurl.equals("") && !imageurl.equals("null"))) {
setParralaxHeader();
} else if (!isTablet()){
thumb.getLayoutParams().height = getActionBarHeight();
FadeBar = false;
} else if (isTablet()){
setParralaxHeader();
thumb.getLayoutParams().height = 0;
}
if (FadeBar) {
mToolbar.getBackground().mutate().setAlpha(0);
/*Helper.setStatusBarColor(DetailActivity.this,
ContextCompat.getColor(this, R.color.black)); Mahmoud comment*/
Helper.setStatusBarColor(DetailActivity.this,
getResources().getColor(R.color.myPrimaryColor));
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
}
// setBackgroundColor(getResources().getColor(R.color.WHITE));
private void setParralaxHeader(){
if(isTablet()){
mScrollableHeaderHeight = coolblue.getLayoutParams().height;
} else {
mScrollableHeaderHeight = thumb.getLayoutParams().height;
/* thumb.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
thumb.getViewTreeObserver().removeOnGlobalLayoutListener(this);
mScrollableHeaderHeight= thumb.getHeight(); //height is ready
}
});*/
mToolbar.setLogo(R.drawable.drawer_header);
}
((TrackingScrollView) findViewById(R.id.scroller))
.setOnScrollChangedListener(new TrackingScrollView.OnScrollChangedListener() {
@Override
public void onScrollChanged(
TrackingScrollView source, int left,
int top, int oldleft, int oldtop) {
handleScroll(top);
}
});
}
private void handleScroll(int top) {
mToolbar.setBackgroundColor(getResources().getColor(R.color.myPrimaryColor));
// mToolbar.setLogo(R.drawable.drawer_header);
/*int scrolledHeaderHeight = top;
if (!isTablet()){
scrolledHeaderHeight = Math.min(mScrollableHeaderHeight, Math.max(0, top));
}
ViewGroup.MarginLayoutParams headerParams = null;
int newHeaderHeight = 0;
if (isTablet()){
headerParams = (ViewGroup.MarginLayoutParams) coolblue
.getLayoutParams();
newHeaderHeight = mScrollableHeaderHeight - (scrolledHeaderHeight) / 2;
} else {
headerParams = (ViewGroup.MarginLayoutParams) thumb
.getLayoutParams();
newHeaderHeight = mScrollableHeaderHeight - scrolledHeaderHeight;
}
if (headerParams.height != newHeaderHeight) {
// Transfer image height to margin top
headerParams.height = newHeaderHeight;
if (!isTablet()){
headerParams.topMargin = scrolledHeaderHeight;
// Invalidate view
thumb.setLayoutParams(headerParams);
} else {
coolblue.setLayoutParams(headerParams);
}
}
Mahmoud comment*/
if (FadeBar) {
/*final int imageheaderHeight = thumb.getHeight()
- getSupportActionBar().getHeight(); Mahmoud comment*/
// t=how far you scrolled
// ratio is from 0,0.1,0.2,...1
/* final float ratio = (float) Math.min(Math.max(top, 0),
imageheaderHeight) / imageheaderHeight; Mahmoud comment*/
// setting the new alpha value from 0-255 or transparent to opaque
// final int newAlpha = (int) (ratio * 255); Mahmoud comment
// if (newAlpha != latestAlpha) {
/*mToolbar.getBackground().mutate().setAlpha(newAlpha);
Helper.setStatusBarColor(DetailActivity.this,
blendColors(ratio, this)); Mahmoud comment*/
/* mToolbar.setBackgroundColor(getResources().getColor(R.color.myPrimaryColor));
mToolbar.setLogo(R.drawable.drawer_header); Mahmoud comment added*/
// }
// latestAlpha = newAlpha;
}
}
private boolean isTablet(){
return getResources().getBoolean(R.bool.isTablet);
}
private int getActionBarHeight() {
int actionBarHeight = getSupportActionBar().getHeight();
if (actionBarHeight != 0)
return actionBarHeight;
final TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
getResources().getDisplayMetrics());
return actionBarHeight;
}
private static int blendColors(float ratio, Context c) {
int color1 = ContextCompat.getColor(c, R.color.myPrimaryDarkColor);
int color2 = ContextCompat.getColor(c, R.color.black);
final float inverseRation = 1f - ratio;
float r = (Color.red(color1) * ratio)
+ (Color.red(color2) * inverseRation);
float g = (Color.green(color1) * ratio)
+ (Color.green(color2) * inverseRation);
float b = (Color.blue(color1) * ratio)
+ (Color.blue(color2) * inverseRation);
return Color.rgb((int) r, (int) g, (int) b);
}
@Override
public void onPause() {
super.onPause();
mToolbar.getBackground().mutate().setAlpha(255);
}
@Override
public void onResume() {
super.onResume();
if (FadeBar)
mToolbar.getBackground().mutate().setAlpha(latestAlpha);
}
}
我已经花了那么多时间,所以请帮助我解决问题
没有错误
答案 0 :(得分:0)
这可能会帮助您尝试检查以下代码,并检查在工具栏中添加的工具栏样式,并且必须将此代码放入页面的onScroll方法中:-
mToolbar.setBackgroundDrawable(new ColorDrawable(0xff00DDED));
mToolbar.setBackgroundColor(0xff00DDED);
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Toast.makeText(this, "dispatch motion", Toast.LENGTH_SHORT).show();
return true;
}
Change actionbar color programmatically more than once
谢谢