style: unify native top action buttons

This commit is contained in:
kris
2026-03-29 19:10:20 +08:00
parent 32960f8ecc
commit e94e91a0f7
4 changed files with 101 additions and 30 deletions

View File

@@ -45,6 +45,8 @@ public abstract class BossScreenActivity extends AppCompatActivity {
BossWindowInsets.applyStatusBarInset(topBarView);
backButton.setOnClickListener(v -> finish());
BossUi.applyTopActionButtonStyle(this, refreshButton, BossUi.TopActionButtonStyle.SECONDARY_TEXT);
BossUi.applyTopActionButtonStyle(this, headerActionButton, BossUi.TopActionButtonStyle.SECONDARY_TEXT);
refreshButton.setOnClickListener(v -> reload());
refreshLayout.setOnRefreshListener(this::reload);
}
@@ -67,6 +69,7 @@ public abstract class BossScreenActivity extends AppCompatActivity {
protected void setHeaderAction(String label, android.view.View.OnClickListener listener) {
headerActionButton.setVisibility(android.view.View.VISIBLE);
headerActionButton.setText(label);
BossUi.applyTopActionButtonStyle(this, headerActionButton, BossUi.TopActionButtonStyle.SECONDARY_TEXT);
headerActionButton.setOnClickListener(listener);
}

View File

@@ -9,6 +9,7 @@ import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.Gravity;
import android.view.View;
@@ -40,6 +41,40 @@ public final class BossUi {
private BossUi() {}
public enum TopActionButtonStyle {
PRIMARY_TEXT,
SECONDARY_TEXT,
COMPACT_ICON
}
public static void applyTopActionButtonStyle(Context context, Button button, TopActionButtonStyle style) {
if (style == TopActionButtonStyle.COMPACT_ICON) {
button.setBackgroundResource(R.drawable.bg_secondary_button);
button.setTextColor(context.getColor(R.color.boss_text_primary));
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
button.setMinWidth(dp(context, 38));
button.setMinimumWidth(dp(context, 38));
button.setMinHeight(dp(context, 36));
button.setMinimumHeight(dp(context, 36));
button.setPadding(dp(context, 12), dp(context, 6), dp(context, 12), dp(context, 6));
return;
}
if (style == TopActionButtonStyle.PRIMARY_TEXT) {
button.setBackgroundResource(R.drawable.bg_primary_button);
button.setTextColor(context.getColor(R.color.boss_surface));
} else {
button.setBackgroundResource(R.drawable.bg_secondary_button);
button.setTextColor(context.getColor(R.color.boss_text_primary));
}
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
button.setMinWidth(0);
button.setMinimumWidth(0);
button.setMinHeight(0);
button.setMinimumHeight(0);
button.setPadding(dp(context, 12), dp(context, 8), dp(context, 12), dp(context, 8));
}
public static LinearLayout buildListRow(
Context context,
String title,

View File

@@ -2,7 +2,6 @@ package com.hyzq.boss;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
@@ -411,35 +410,14 @@ public class MainActivity extends AppCompatActivity {
private void configureTopAction(WechatSurfaceMapper.RootTopAction action) {
refreshButton.setText(action.label);
if (action.compactStyle) {
refreshButton.setBackgroundResource(R.drawable.bg_secondary_button);
refreshButton.setTextColor(getColor(R.color.boss_text_primary));
refreshButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
refreshButton.setMinWidth(BossUi.dp(this, 38));
refreshButton.setMinimumWidth(BossUi.dp(this, 38));
refreshButton.setMinHeight(BossUi.dp(this, 36));
refreshButton.setMinimumHeight(BossUi.dp(this, 36));
refreshButton.setPadding(
BossUi.dp(this, 12),
BossUi.dp(this, 6),
BossUi.dp(this, 12),
BossUi.dp(this, 6)
);
return;
}
refreshButton.setBackgroundResource(action.primaryStyle ? R.drawable.bg_primary_button : R.drawable.bg_secondary_button);
refreshButton.setTextColor(getColor(action.primaryStyle ? R.color.boss_surface : R.color.boss_green));
refreshButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
refreshButton.setMinWidth(0);
refreshButton.setMinimumWidth(0);
refreshButton.setMinHeight(0);
refreshButton.setMinimumHeight(0);
refreshButton.setPadding(
BossUi.dp(this, 12),
BossUi.dp(this, 8),
BossUi.dp(this, 12),
BossUi.dp(this, 8)
BossUi.applyTopActionButtonStyle(
this,
refreshButton,
action.compactStyle
? BossUi.TopActionButtonStyle.COMPACT_ICON
: action.primaryStyle
? BossUi.TopActionButtonStyle.PRIMARY_TEXT
: BossUi.TopActionButtonStyle.SECONDARY_TEXT
);
}

View File

@@ -0,0 +1,55 @@
package com.hyzq.boss;
import static org.junit.Assert.assertEquals;
import android.content.Context;
import android.util.TypedValue;
import android.widget.Button;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@Config(sdk = 34)
public class BossUiTopActionStyleTest {
@Test
public void applyCompactTopActionStyle_usesWechatLikeIconSizing() {
Context context = RuntimeEnvironment.getApplication();
Button button = new Button(context);
BossUi.applyTopActionButtonStyle(context, button, BossUi.TopActionButtonStyle.COMPACT_ICON);
assertEquals(context.getColor(R.color.boss_text_primary), button.getCurrentTextColor());
assertEquals(BossUi.dp(context, 38), button.getMinWidth());
assertEquals(BossUi.dp(context, 36), button.getMinHeight());
assertEquals(BossUi.dp(context, 12), button.getPaddingLeft());
assertEquals(BossUi.dp(context, 6), button.getPaddingTop());
assertEquals(
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, context.getResources().getDisplayMetrics()),
button.getTextSize(),
0.01f
);
}
@Test
public void applySecondaryTopActionStyle_usesDarkTextAndTightPadding() {
Context context = RuntimeEnvironment.getApplication();
Button button = new Button(context);
BossUi.applyTopActionButtonStyle(context, button, BossUi.TopActionButtonStyle.SECONDARY_TEXT);
assertEquals(context.getColor(R.color.boss_text_primary), button.getCurrentTextColor());
assertEquals(0, button.getMinWidth());
assertEquals(0, button.getMinHeight());
assertEquals(BossUi.dp(context, 12), button.getPaddingLeft());
assertEquals(BossUi.dp(context, 8), button.getPaddingTop());
assertEquals(
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, context.getResources().getDisplayMetrics()),
button.getTextSize(),
0.01f
);
}
}