react-native-navigation的迁移库

TopTabsStyleHelper.java 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package com.reactnativenavigation.views.toptabs;
  2. import android.content.res.ColorStateList;
  3. import android.graphics.Typeface;
  4. import android.view.ViewGroup;
  5. import android.widget.TextView;
  6. import com.reactnativenavigation.parse.params.Color;
  7. import com.reactnativenavigation.parse.params.Number;
  8. import com.reactnativenavigation.utils.Task;
  9. import com.reactnativenavigation.utils.ViewUtils;
  10. class TopTabsStyleHelper {
  11. private TopTabs topTabs;
  12. TopTabsStyleHelper(TopTabs topTabs) {
  13. this.topTabs = topTabs;
  14. }
  15. void applyTopTabsFontSize(Number fontSize) {
  16. if (!fontSize.hasValue()) return;
  17. for (int i = 0; i < topTabs.getTabCount(); i++) {
  18. applyOnTabTitle(i, (title) -> title.setTextSize(fontSize.get()));
  19. }
  20. }
  21. void applyTopTabsColors(Color selected, Color unselected) {
  22. ColorStateList originalColors = topTabs.getTabTextColors();
  23. int selectedTabColor = originalColors != null ? originalColors.getColorForState(topTabs.getSelectedTabColors(), -1) : -1;
  24. int tabTextColor = originalColors != null ? originalColors.getColorForState(topTabs.getDefaultTabColors(), -1) : -1;
  25. if (selected.hasValue()) {
  26. selectedTabColor = selected.get();
  27. }
  28. if (unselected.hasValue()) {
  29. tabTextColor = unselected.get();
  30. }
  31. topTabs.setTabTextColors(tabTextColor, selectedTabColor);
  32. }
  33. void setFontFamily(int tabIndex, Typeface fontFamily) {
  34. applyOnTabTitle(tabIndex, (title) -> title.setTypeface(fontFamily));
  35. }
  36. private void applyOnTabTitle(int tabIndex, Task<TextView> action) {
  37. TextView title = ViewUtils.findChildByClass(getTabView(tabIndex), TextView.class);
  38. if (title != null) {
  39. action.run(title);
  40. }
  41. }
  42. private ViewGroup getTabView(int tabIndex) {
  43. return (ViewGroup) ((ViewGroup) topTabs.getChildAt(0)).getChildAt(tabIndex);
  44. }
  45. }