Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugFix] 修复AS预览模式不显示layout问题 #71

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
2. 支持自定义视图;
3. 支持沉浸式标题栏;

|首页|快速预览|
|首页|快速预览|
|:---:|:---:|
|<img src="screenshots/titlebar_home.png" alt="screenshot" width="300">|<img src="screenshots/20171201_182534.gif" alt="screenshot" width="300">|

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.novoda:bintray-release:0.9.1'
classpath 'com.android.tools.build:gradle:4.2.2'
classpath 'com.novoda:bintray-release:0.8.1'
}
}

Expand Down
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Fri Jan 19 10:03:01 CST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@ public static int px2sp(Context context, float pxValue) {
}

public static DisplayMetrics getDisplayMetrics(Context context) {
Activity activity;
Activity activity = null;
if (!(context instanceof Activity) && context instanceof ContextWrapper) {
activity = (Activity) ((ContextWrapper) context).getBaseContext();
} else {
//[bugFix] 强制类型转换会引发AS预览无法加载布局
//} else {
} else if (context instanceof Activity){
activity = (Activity) context;
}
DisplayMetrics metrics = new DisplayMetrics();
if (activity == null) {
return null;
}
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
return metrics;
}
Expand All @@ -70,7 +75,11 @@ public static DisplayMetrics getDisplayMetrics(Context context) {
*/
public static int[] getScreenPixelSize(Context context) {
DisplayMetrics metrics = getDisplayMetrics(context);
return new int[]{metrics.widthPixels, metrics.heightPixels};
if (metrics == null) {
return new int[]{0, 0};
} else {
return new int[]{metrics.widthPixels, metrics.heightPixels};
}
}

public static void hideSoftInputKeyBoard(Context context, View focusView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.IntDef;

import com.wuhenzhizao.titlebar.R;
import com.wuhenzhizao.titlebar.statusbar.StatusBarUtils;
import com.wuhenzhizao.titlebar.utils.ScreenUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
* 通用标题栏
* <p/>
Expand Down Expand Up @@ -122,6 +128,7 @@ public class CommonTitleBar extends RelativeLayout implements View.OnClickListen
private int titleBarColor; // 标题栏背景颜色
private int titleBarHeight; // 标题栏高度
private int statusBarColor; // 状态栏颜色
@StatusBarMode
private int statusBarMode; // 状态栏模式

private boolean showBottomLine; // 是否显示底部分割线
Expand Down Expand Up @@ -208,7 +215,7 @@ private void loadAttributes(Context context, AttributeSet attrs) {
leftType = array.getInt(R.styleable.CommonTitleBar_leftType, TYPE_LEFT_NONE);
if (leftType == TYPE_LEFT_TEXTVIEW) {
leftText = array.getString(R.styleable.CommonTitleBar_leftText);
leftTextColor = array.getColor(R.styleable.CommonTitleBar_leftTextColor, getResources().getColor(R.color.comm_titlebar_text_selector));
leftTextColor = array.getColor(R.styleable.CommonTitleBar_leftTextColor, getDefaultTextColor());
leftTextSize = array.getDimension(R.styleable.CommonTitleBar_leftTextSize, ScreenUtils.dp2PxInt(context, 16));
leftDrawable = array.getResourceId(R.styleable.CommonTitleBar_leftDrawable, 0);
leftDrawablePadding = array.getDimension(R.styleable.CommonTitleBar_leftDrawablePadding, 5);
Expand All @@ -221,8 +228,7 @@ private void loadAttributes(Context context, AttributeSet attrs) {
rightType = array.getInt(R.styleable.CommonTitleBar_rightType, TYPE_RIGHT_NONE);
if (rightType == TYPE_RIGHT_TEXTVIEW) {
rightText = array.getString(R.styleable.CommonTitleBar_rightText);
rightTextColor = array.getColor(R.styleable.CommonTitleBar_rightTextColor, getResources().getColor(R.color.comm_titlebar_text_selector));
rightTextSize = array.getDimension(R.styleable.CommonTitleBar_rightTextSize, ScreenUtils.dp2PxInt(context, 16));
rightTextColor = array.getColor(R.styleable.CommonTitleBar_rightTextColor, getDefaultTextColor());
} else if (rightType == TYPE_RIGHT_IMAGEBUTTON) {
rightImageResource = array.getResourceId(R.styleable.CommonTitleBar_rightImageResource, 0);
} else if (rightType == TYPE_RIGHT_CUSTOM_VIEW) {
Expand All @@ -249,6 +255,15 @@ private void loadAttributes(Context context, AttributeSet attrs) {
array.recycle();
}

@ColorInt
private int getDefaultTextColor() {
if (isInEditMode()) {
return Color.parseColor("#d8d8d8");
} else {
return getResources().getColor(R.color.comm_titlebar_text_selector);
}
}

private final int MATCH_PARENT = ViewGroup.LayoutParams.MATCH_PARENT;
private final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;

Expand All @@ -261,7 +276,10 @@ private void initGlobalViews(Context context) {
ViewGroup.LayoutParams globalParams = new ViewGroup.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
setLayoutParams(globalParams);

boolean transparentStatusBar = StatusBarUtils.supportTransparentStatusBar();
//[optimize] 预览模式下不显示状态栏(fillStatusBar="true")
//[bugFix] 在editMode 下 无法使用 getRuntime().exec()
//boolean transparentStatusBar =StatusBarUtils.supportTransparentStatusBar();
boolean transparentStatusBar = isInEditMode() ? false : StatusBarUtils.supportTransparentStatusBar();

// 构建标题栏填充视图
if (fillStatusBar && transparentStatusBar) {
Expand Down Expand Up @@ -627,10 +645,14 @@ protected void onAttachedToWindow() {
private void setUpImmersionTitleBar() {
Window window = getWindow();
if (window == null) return;
//[bugFix] 修复AS预览模式不显示layout问题
if (isInEditMode()) {
return;
}
// 设置状态栏背景透明
StatusBarUtils.transparentStatusBar(window);
// 设置图标主题
if (statusBarMode == 0) {
if (statusBarMode == STATUS_BAR_MODE_DARK) {
StatusBarUtils.setDarkMode(window);
} else {
StatusBarUtils.setLightMode(window);
Expand All @@ -639,10 +661,12 @@ private void setUpImmersionTitleBar() {

private Window getWindow() {
Context context = getContext();
Activity activity;
Activity activity = null;
if (context instanceof Activity) {
activity = (Activity) context;
} else {
//[bugFix] 强制类型转换会导致在新的AS版本中无法预览显示
//} else {
} else if (context instanceof ContextWrapper) {
activity = (Activity) ((ContextWrapper) context).getBaseContext();
}
if (activity != null) {
Expand Down Expand Up @@ -789,12 +813,33 @@ public void showStatusBar(boolean show) {
public void toggleStatusBarMode() {
Window window = getWindow();
if (window == null) return;
if (isInEditMode()) return;
StatusBarUtils.transparentStatusBar(window);
if (statusBarMode == 0) {
statusBarMode = 1;
if (statusBarMode == STATUS_BAR_MODE_DARK) {
statusBarMode = STATUS_BAR_MODE_LIGHT;
StatusBarUtils.setLightMode(window);
} else {
statusBarMode = STATUS_BAR_MODE_DARK;
StatusBarUtils.setDarkMode(window);
}
}

/**
* 改变状态栏显示模式
*
* @param mode 状态栏显示模式 若传入值与原来值一致,则不做处理
*/
public void setStatusBarMode(@StatusBarMode int mode) {
if (statusBarMode == mode) return;

Window window = getWindow();
if (window == null) return;
if (isInEditMode()) return;
StatusBarUtils.transparentStatusBar(window);
this.statusBarMode = mode;
if (statusBarMode == STATUS_BAR_MODE_DARK) {
StatusBarUtils.setLightMode(window);
} else {
statusBarMode = 0;
StatusBarUtils.setDarkMode(window);
}
}
Expand Down Expand Up @@ -1056,4 +1101,18 @@ public interface OnTitleBarListener {
public interface OnTitleBarDoubleClickListener {
void onClicked(View v);
}


/**
* 状态栏模式
*/

public static final int STATUS_BAR_MODE_DARK = 0; // 状态栏暗色模式
public static final int STATUS_BAR_MODE_LIGHT = 1; // 状态栏亮色模式

@IntDef({STATUS_BAR_MODE_DARK, STATUS_BAR_MODE_LIGHT})
@Retention(RetentionPolicy.SOURCE)
public @interface StatusBarMode {
}

}
2 changes: 1 addition & 1 deletion library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<attr name="rightTextColor" format="color" />
<!-- TextView 字体大小, 对应rightType_TextView -->
<attr name="rightTextSize" format="dimension" />
<!-- ImageView 资源, 对应rightType_ImageBotton -->
<!-- ImageView 资源, 对应rightType_ImageBottom -->
<attr name="rightImageResource" format="reference" />
<!-- 右边自定义布局, 对应rightType_CustomView -->
<attr name="rightCustomView" format="reference" />
Expand Down