Skip to content

Commit

Permalink
add statusbarspace
Browse files Browse the repository at this point in the history
  • Loading branch information
pichsy committed Mar 2, 2021
1 parent 4ae768a commit cb7bf83
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 10 deletions.
5 changes: 4 additions & 1 deletion app/src/main/java/com/pichs/app/xwidget/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.pichs.common.widget.cardview.XCardButton;
import com.pichs.common.widget.switcher.XSwitchButton;
import com.pichs.common.widget.utils.XStatusBarHelper;
import com.pichs.common.widget.utils.XTypefaceHelper;
import com.pichs.common.widget.view.XButton;

Expand All @@ -22,12 +23,14 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
XStatusBarHelper.translucent(this);
XStatusBarHelper.setStatusBarDarkMode(this);
setContentView(R.layout.activity_main);

XCardButton btn = findViewById(R.id.btn1);
XSwitchButton swb = findViewById(R.id.swb);

swb.setThumbColor(Color.BLACK, Color.BLACK, Color.BLACK, Color.BLACK );
swb.setThumbColor(Color.BLACK, Color.BLACK, Color.BLACK, Color.BLACK);
swb.setBackgroundColor(Color.GREEN, Color.CYAN);
btn.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down
28 changes: 24 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,33 @@
android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">


<com.pichs.common.widget.view.XStatusBarView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f00" />


<LinearLayout
android:layout_width="match_parent"
android:background="#0ff"
android:layout_height="wrap_content">

<com.pichs.common.widget.space.XStatusBarSpace
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f00" />

</LinearLayout>

<com.pichs.common.widget.cardview.XCardButton
android:id="@+id/btn1"
android:layout_width="180dp"
Expand Down Expand Up @@ -55,8 +75,8 @@
app:xp_ignoreGlobalTypeface="true" />

<com.pichs.common.widget.switcher.XSwitchButton
android:layout_width="180dp"
android:id="@+id/swb"
android:layout_width="180dp"
android:layout_height="60dp"
android:checked="false"
android:textColor="@color/white"
Expand All @@ -74,12 +94,12 @@
android:layout_height="40dp"
android:layout_marginTop="20dp"
app:xp_shine_big_shine_color="#F10C0C"
app:xp_shine_small_shine_color="#3CC63C"
app:xp_shine_checked_color="#0195EB"
app:xp_shine_count="10"
app:xp_shine_enable_flashing="true"
app:xp_shine_checked_color="#0195EB"
app:xp_shine_icon_image="@drawable/ic_start"
app:xp_shine_normal_color="#C71FEA" />
app:xp_shine_normal_color="#C71FEA"
app:xp_shine_small_shine_color="#3CC63C" />

<Space
android:layout_width="match_parent"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Xwidget" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<style name="Theme.Xwidget" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Xwidget" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<style name="Theme.Xwidget" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.pichs.common.widget.space;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.pichs.common.widget.utils.XStatusBarHelper;

/**
* StatusBarSpace
* 不会绘制的状态栏占位 Space
*/
public class XStatusBarSpace extends View {

private final int statusBarHeight;

public XStatusBarSpace(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
if (getVisibility() == VISIBLE) {
setVisibility(INVISIBLE);
}
statusBarHeight = XStatusBarHelper.getStatusBarHeight(context);
}

public XStatusBarSpace(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public XStatusBarSpace(Context context) {
this(context, null);
}

/**
* Compare to: {@link View#getDefaultSize(int, int)}
* If mode is AT_MOST, return the child size instead of the parent size
* (unless it is too big).
*/
private static int getDefaultSize2(int size, int measureSpec) {
int result = size;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

switch (specMode) {
case MeasureSpec.UNSPECIFIED:
result = size;
break;
case MeasureSpec.AT_MOST:
result = Math.min(size, specSize);
break;
case MeasureSpec.EXACTLY:
result = specSize;
break;
}
return result;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(
getDefaultSize2(getSuggestedMinimumWidth(), widthMeasureSpec),
statusBarHeight);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ public class XDisplayHelper {
*/
private static Boolean sHasCamera = null;

// private static int[] sPortraitRealSizeCache = null;
// private static int[] sLandscapeRealSizeCache = null;

/**
* 获取 DisplayMetrics
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.pichs.common.widget.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.pichs.common.widget.utils.XStatusBarHelper;

/**
* StatusBarView
* 绘制的状态栏高度 View,
* 可设置背景色
*/
public class XStatusBarView extends XView {

private final int statusBarHeight;

public XStatusBarView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
statusBarHeight = XStatusBarHelper.getStatusBarHeight(context);
}

public XStatusBarView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public XStatusBarView(Context context) {
this(context, null);
}

/**
* Compare to: {@link View#getDefaultSize(int, int)}
* If mode is AT_MOST, return the child size instead of the parent size
* (unless it is too big).
*/
private static int getDefaultSize2(int size, int measureSpec) {
int result = size;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

switch (specMode) {
case MeasureSpec.UNSPECIFIED:
result = size;
break;
case MeasureSpec.AT_MOST:
result = Math.min(size, specSize);
break;
case MeasureSpec.EXACTLY:
result = specSize;
break;
}
return result;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(
getDefaultSize2(getSuggestedMinimumWidth(), widthMeasureSpec),
statusBarHeight);
}
}
35 changes: 35 additions & 0 deletions widget/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,41 @@

</declare-styleable>

<!-- XStatusBarView -->
<declare-styleable name="XStatusBarView">

<!-- 背景色渐变色,正常时显示 -->
<attr name="android:background" />
<attr name="xp_backgroundGradientStartColor" />
<attr name="xp_backgroundGradientEndColor" />
<attr name="xp_backgroundGradientOrientation" />

<!-- 按压时 背景色 -->
<attr name="xp_pressedBackground" />
<attr name="xp_pressedBackgroundStartColor" />
<attr name="xp_pressedBackgroundEndColor" />
<attr name="xp_pressedBackgroundOrientation" />

<!-- 选中时 背景色 -->
<attr name="xp_checkedBackground" />
<attr name="xp_checkedBackgroundStartColor" />
<attr name="xp_checkedBackgroundEndColor" />
<attr name="xp_checkedBackgroundOrientation" />

<!-- 活跃时 背景色 -->
<attr name="xp_activatedBackground" />
<attr name="xp_activatedBackgroundStartColor" />
<attr name="xp_activatedBackgroundEndColor" />
<attr name="xp_activatedBackgroundOrientation" />

<!-- 不可用时 背景色 -->
<attr name="xp_unEnabledBackground" />
<attr name="xp_unEnabledBackgroundStartColor" />
<attr name="xp_unEnabledBackgroundEndColor" />
<attr name="xp_unEnabledBackgroundOrientation" />

</declare-styleable>

<!-- XViewGroup -->
<declare-styleable name="XViewGroup">

Expand Down

0 comments on commit cb7bf83

Please sign in to comment.