• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Android更换主题

武飞扬头像
孜燃
帮助1

类似效果

学新通

简单写法 主题属性custom_theme_attrs.xml

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <resources>
  3.  
    <!-- 自定义属性 -->
  4.  
    <attr name="text_color" format="color" />
  5.  
     
  6.  
    <!-- 日间主题 -->
  7.  
    <style name="DayTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
  8.  
    <item name="text_color">@color/black_tx_color</item>
  9.  
    </style>
  10.  
     
  11.  
    <!-- 夜间主题 -->
  12.  
    <style name="NightTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
  13.  
    <item name="text_color">@color/white_tx_color</item>
  14.  
    </style>
  15.  
    </resources>
学新通

颜色定义custom_theme_colors.xml

  1.  
    <resources>
  2.  
    <!-- 日间模式 -->
  3.  
    <color name="black_tx_color">#333333</color>
  4.  
     
  5.  
    <!-- 夜间模式 -->
  6.  
    <color name="white_tx_color">#f0f0f0</color>
  7.  
    </resources>

布局

  1.  
    <LinearLayout
  2.  
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    xmlns:tools="http://schemas.android.com/tools"
  4.  
    android:id="@ id/root_view"
  5.  
    android:layout_width="match_parent"
  6.  
    android:layout_height="match_parent"
  7.  
    >
  8.  
     
  9.  
     
  10.  
    <Button
  11.  
    android:id="@ id/change_btn"
  12.  
    android:layout_width="match_parent"
  13.  
    android:layout_height="wrap_content"
  14.  
    android:layout_marginTop="20dp"
  15.  
    android:text="给我改主题"
  16.  
    android:textColor="@color/black"
  17.  
    />
  18.  
     
  19.  
    <Button
  20.  
    android:textColor="@color/black"
  21.  
    android:id="@ id/second_btn"
  22.  
    android:layout_width="match_parent"
  23.  
    android:layout_height="wrap_content"
  24.  
    android:layout_below="@id/change_btn"
  25.  
    android:layout_marginTop="20dp"
  26.  
    android:text="Recyclerview"
  27.  
    />
  28.  
     
  29.  
    <TextView
  30.  
    android:id="@ id/tv_name"
  31.  
    android:layout_width="match_parent"
  32.  
    android:layout_height="wrap_content"
  33.  
    android:layout_marginTop="120dp"
  34.  
    android:gravity="center"
  35.  
    android:text="aaaaaaaa"
  36.  
    android:textColor="?attr/text_color"
  37.  
    android:textSize="20dp" />
  38.  
     
  39.  
     
  40.  
     
  41.  
    </LinearLayout>
学新通

代码片段

  1.  
    //切换主题
  2.  
    public class MainActivity5 extends AppCompatActivity {
  3.  
    Button change_btn;
  4.  
    Button second_btn;
  5.  
    TextView tv_name;
  6.  
    private Colorful mColorful;
  7.  
     
  8.  
     
  9.  
    @Override
  10.  
    protected void onCreate(Bundle savedInstanceState) {
  11.  
    setTheme(R.style.DayTheme);
  12.  
    super.onCreate(savedInstanceState);
  13.  
    setContentView(R.layout.activity_main);
  14.  
    change_btn = (Button) findViewById(R.id.change_btn);
  15.  
    second_btn = (Button) findViewById(R.id.second_btn);
  16.  
    tv_name = (TextView) findViewById(R.id.tv_name);
  17.  
     
  18.  
    change_btn.setOnClickListener(new View.OnClickListener() {
  19.  
    @Override
  20.  
    public void onClick(View view) {
  21.  
    animChangeColor(R.style.NightTheme);
  22.  
    }
  23.  
    });
  24.  
    second_btn.setOnClickListener(new View.OnClickListener() {
  25.  
    @Override
  26.  
    public void onClick(View view) {
  27.  
    animChangeColor(R.style.DayTheme);
  28.  
    }
  29.  
    });
  30.  
     
  31.  
    //Recyclerview
  32.  
    // ViewGroupSetter rvSetter = new ViewGroupSetter(mNightRv, R.attr.root_view_bg);
  33.  
    // rvSetter.childViewTextColor(R.id.category_desc, R.attr.one_text_bg);
  34.  
    // rvSetter.childViewTextColor(R.id.category_author, R.attr.two_text_bg);
  35.  
    // rvSetter.childViewTextColor(R.id.category_date, R.attr.two_text_bg);
  36.  
    // rvSetter.childViewBgColor(R.id.night_rl, R.attr.cardview_bg);
  37.  
     
  38.  
    // 构建Colorful对象
  39.  
    mColorful = new Colorful.Builder(this)
  40.  
    .textColor(R.id.tv_name, R.attr.text_color) // 设置文本颜色
  41.  
    // .setter(rvSetter)
  42.  
    .create();
  43.  
     
  44.  
    }
  45.  
     
  46.  
    boolean isNight = false ;
  47.  
     
  48.  
    // 切换主题
  49.  
    private void changeThemeWithColorful() {
  50.  
    if (!isNight) {
  51.  
    mColorful.setTheme(R.style.DayTheme);
  52.  
    } else {
  53.  
    mColorful.setTheme(R.style.NightTheme);
  54.  
    }
  55.  
    isNight = !isNight;
  56.  
    }
  57.  
     
  58.  
    /**
  59.  
    * 给夜间模式增加一个动画,颜色渐变
  60.  
    *
  61.  
    * @param newTheme
  62.  
    */
  63.  
    private void animChangeColor(final int newTheme) {
  64.  
    final View rootView = getWindow().getDecorView();
  65.  
    rootView.setDrawingCacheEnabled(true);
  66.  
    rootView.buildDrawingCache(true);
  67.  
     
  68.  
    final Bitmap localBitmap = Bitmap.createBitmap(rootView.getDrawingCache());
  69.  
    rootView.setDrawingCacheEnabled(false);
  70.  
    if (null != localBitmap && rootView instanceof ViewGroup) {
  71.  
    final View tmpView = new View(this);
  72.  
    tmpView.setBackgroundDrawable(new BitmapDrawable(getResources(), localBitmap));
  73.  
    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
  74.  
    .LayoutParams.MATCH_PARENT);
  75.  
    ((ViewGroup) rootView).addView(tmpView, params);
  76.  
    tmpView.animate().alpha(0).setDuration(400).setListener(new Animator.AnimatorListener() {
  77.  
    @Override
  78.  
    public void onAnimationStart(Animator animation) {
  79.  
    mColorful.setTheme(newTheme);
  80.  
    System.gc();
  81.  
    }
  82.  
     
  83.  
    @Override
  84.  
    public void onAnimationEnd(Animator animation) {
  85.  
    ((ViewGroup) rootView).removeView(tmpView);
  86.  
    localBitmap.recycle();
  87.  
    }
  88.  
     
  89.  
    @Override
  90.  
    public void onAnimationCancel(Animator animation) {
  91.  
     
  92.  
    }
  93.  
     
  94.  
    @Override
  95.  
    public void onAnimationRepeat(Animator animation) {
  96.  
     
  97.  
    }
  98.  
    }).start();
  99.  
    }
  100.  
    }
  101.  
     
  102.  
    private Disposable countdownDisposable;
  103.  
     
  104.  
    @Override
  105.  
    protected void onDestroy() {
  106.  
    super.onDestroy();
  107.  
    countdownDisposable.dispose();
  108.  
    }
  109.  
    }
学新通

用到的工具  Colorful

  1.  
    public class Colorful
  2.  
    {
  3.  
    /**
  4.  
    * Colorful Builder
  5.  
    */
  6.  
    Builder mBuilder;
  7.  
     
  8.  
    /**
  9.  
    * private constructor
  10.  
    *
  11.  
    * @param builder
  12.  
    */
  13.  
    private Colorful(Builder builder)
  14.  
    {
  15.  
    mBuilder = builder;
  16.  
    }
  17.  
     
  18.  
    /**
  19.  
    * 设置新的主题
  20.  
    *
  21.  
    * @param newTheme
  22.  
    */
  23.  
    public void setTheme(int newTheme)
  24.  
    {
  25.  
    mBuilder.setTheme(newTheme);
  26.  
    }
  27.  
     
  28.  
    /**
  29.  
    * 构建Colorful的Builder对象
  30.  
    *
  31.  
    * @author mrsimple
  32.  
    */
  33.  
    public static class Builder
  34.  
    {
  35.  
    /**
  36.  
    * 存储了视图和属性资源id的关系表
  37.  
    */
  38.  
    List<ViewSetter> mElements = new ArrayList<>();
  39.  
    /**
  40.  
    * 目标Activity
  41.  
    */
  42.  
    Activity mActivity;
  43.  
     
  44.  
    /**
  45.  
    * @param activity
  46.  
    */
  47.  
    public Builder(Activity activity)
  48.  
    {
  49.  
    mActivity = activity;
  50.  
    }
  51.  
     
  52.  
    /**
  53.  
    * @param fragment
  54.  
    */
  55.  
    public Builder(Fragment fragment)
  56.  
    {
  57.  
    mActivity = fragment.getActivity();
  58.  
    }
  59.  
     
  60.  
    private View findViewById(int viewId)
  61.  
    {
  62.  
    return mActivity.findViewById(viewId);
  63.  
    }
  64.  
     
  65.  
    /**
  66.  
    * 将View id与存储该view背景色的属性进行绑定
  67.  
    *
  68.  
    * @param viewId 控件id
  69.  
    * @param colorId 颜色属性id
  70.  
    * @return
  71.  
    */
  72.  
    public Builder backgroundColor(int viewId, int colorId)
  73.  
    {
  74.  
    mElements.add(new ViewBackgroundColorSetter(findViewById(viewId), colorId));
  75.  
    return this;
  76.  
    }
  77.  
     
  78.  
    /**
  79.  
    * 将View id与存储该view背景Drawable的属性进行绑定
  80.  
    *
  81.  
    * @param viewId 控件id
  82.  
    * @param drawableId Drawable属性id
  83.  
    * @return
  84.  
    */
  85.  
    public Builder backgroundDrawable(int viewId, int drawableId)
  86.  
    {
  87.  
    mElements.add(new ViewBackgroundDrawableSetter(
  88.  
    findViewById(viewId), drawableId));
  89.  
    return this;
  90.  
    }
  91.  
     
  92.  
    /**
  93.  
    * 将TextView id与存储该TextView文本颜色的属性进行绑定
  94.  
    *
  95.  
    * @param viewId TextView或者TextView子类控件的id
  96.  
    * @param colorId 颜色属性id
  97.  
    * @return
  98.  
    */
  99.  
    public Builder textColor(int viewId, int colorId)
  100.  
    {
  101.  
    TextView textView = (TextView) findViewById(viewId);
  102.  
    mElements.add(new TextColorSetter(textView, colorId));
  103.  
    return this;
  104.  
    }
  105.  
     
  106.  
    /**
  107.  
    * 用户手动构造并且添加Setter
  108.  
    *
  109.  
    * @param setter 用户自定义的Setter
  110.  
    * @return
  111.  
    */
  112.  
    public Builder setter(ViewSetter setter)
  113.  
    {
  114.  
    mElements.add(setter);
  115.  
    return this;
  116.  
    }
  117.  
     
  118.  
    /**
  119.  
    * 设置新的主题
  120.  
    *
  121.  
    * @param newTheme
  122.  
    */
  123.  
    protected void setTheme(int newTheme)
  124.  
    {
  125.  
    mActivity.setTheme(newTheme);
  126.  
    makeChange(newTheme);
  127.  
    }
  128.  
     
  129.  
    /**
  130.  
    * 修改各个视图绑定的属性
  131.  
    */
  132.  
    private void makeChange(int themeId)
  133.  
    {
  134.  
    Theme curTheme = mActivity.getTheme();
  135.  
    for (ViewSetter setter : mElements)
  136.  
    {
  137.  
    setter.setValue(curTheme, themeId);
  138.  
    }
  139.  
    }
  140.  
     
  141.  
    /**
  142.  
    * 创建Colorful对象
  143.  
    *
  144.  
    * @return
  145.  
    */
  146.  
    public Colorful create()
  147.  
    {
  148.  
    return new Colorful(this);
  149.  
    }
  150.  
    }
  151.  
    }
学新通

RecyclerViewSetter 

  1.  
    public class RecyclerViewSetter extends ViewGroupSetter
  2.  
    {
  3.  
     
  4.  
    public RecyclerViewSetter(ViewGroup targetView, int resId)
  5.  
    {
  6.  
    super(targetView, resId);
  7.  
    }
  8.  
     
  9.  
    public RecyclerViewSetter(ViewGroup targetView)
  10.  
    {
  11.  
    super(targetView);
  12.  
    }
  13.  
     
  14.  
    @Override
  15.  
    protected void clearRecyclerViewRecyclerBin(View rootView)
  16.  
    {
  17.  
    super.clearRecyclerViewRecyclerBin(rootView);
  18.  
    ((RecyclerView) rootView).getRecycledViewPool().clear();
  19.  
    }
  20.  
     
  21.  
    }
学新通

TextColorSetter

  1.  
    public class TextColorSetter extends ViewSetter
  2.  
    {
  3.  
    public TextColorSetter(TextView textView, int resId)
  4.  
    {
  5.  
    super(textView, resId);
  6.  
    }
  7.  
     
  8.  
    public TextColorSetter(int viewId, int resId)
  9.  
    {
  10.  
    super(viewId, resId);
  11.  
    }
  12.  
     
  13.  
    @Override
  14.  
    public void setValue(Theme newTheme, int themeId)
  15.  
    {
  16.  
    if (mView == null)
  17.  
    {
  18.  
    return;
  19.  
    }
  20.  
    Log.e("color123", "setValue(TextColorSetter.java:29)");
  21.  
    ((TextView) mView).setTextColor(getColor(newTheme));
  22.  
    }
  23.  
    }
学新通

ViewBackgroundColorSetter 

  1.  
    public class ViewBackgroundColorSetter extends ViewSetter
  2.  
    {
  3.  
    public ViewBackgroundColorSetter(View target, int resId)
  4.  
    {
  5.  
    super(target, resId);
  6.  
    }
  7.  
     
  8.  
    public ViewBackgroundColorSetter(int viewId, int resId)
  9.  
    {
  10.  
    super(viewId, resId);
  11.  
    }
  12.  
     
  13.  
    @Override
  14.  
    public void setValue(Theme newTheme, int themeId)
  15.  
    {
  16.  
    if (mView != null)
  17.  
    {
  18.  
    Log.e("color123","setValue(ViewBackgroundColorSetter.java:29)");
  19.  
    mView.setBackgroundColor(getColor(newTheme));
  20.  
    }
  21.  
    }
  22.  
    }
学新通

ViewBackgroundDrawableSetter

  1.  
    public class ViewBackgroundDrawableSetter extends ViewSetter
  2.  
    {
  3.  
    public ViewBackgroundDrawableSetter(View targetView, int resId)
  4.  
    {
  5.  
    super(targetView, resId);
  6.  
    }
  7.  
     
  8.  
     
  9.  
    public ViewBackgroundDrawableSetter(int viewId, int resId)
  10.  
    {
  11.  
    super(viewId, resId);
  12.  
    }
  13.  
     
  14.  
    @SuppressWarnings("deprecation")
  15.  
    @Override
  16.  
    public void setValue(Theme newTheme, int themeId)
  17.  
    {
  18.  
    if (mView == null)
  19.  
    {
  20.  
    return;
  21.  
    }
  22.  
    TypedArray a = newTheme.obtainStyledAttributes(themeId,
  23.  
    new int[]{mAttrResId});
  24.  
    int attributeResourceId = a.getResourceId(0, 0);
  25.  
    Drawable drawable = mView.getResources().getDrawable(
  26.  
    attributeResourceId);
  27.  
    a.recycle();
  28.  
    mView.setBackgroundDrawable(drawable);
  29.  
    }
  30.  
    }
学新通

ViewGroupSetter 

  1.  
    public class ViewGroupSetter extends ViewSetter
  2.  
    {
  3.  
    /**
  4.  
    * ListView的子试图的Setter
  5.  
    */
  6.  
    protected Set<ViewSetter> mItemViewSetters = new HashSet<ViewSetter>();
  7.  
     
  8.  
    /**
  9.  
    * @param targetView
  10.  
    * @param resId
  11.  
    */
  12.  
    public ViewGroupSetter(ViewGroup targetView, int resId)
  13.  
    {
  14.  
    super(targetView, resId);
  15.  
    }
  16.  
     
  17.  
    public ViewGroupSetter(ViewGroup targetView)
  18.  
    {
  19.  
    super(targetView, 0);
  20.  
    }
  21.  
     
  22.  
    /**
  23.  
    * 设置View的背景色
  24.  
    *
  25.  
    * @param viewId
  26.  
    * @param colorId
  27.  
    * @return
  28.  
    */
  29.  
    public ViewGroupSetter childViewBgColor(int viewId, int colorId)
  30.  
    {
  31.  
    mItemViewSetters.add(new ViewBackgroundColorSetter(viewId, colorId));
  32.  
    return this;
  33.  
    }
  34.  
     
  35.  
    /**
  36.  
    * 设置View的drawable背景
  37.  
    *
  38.  
    * @param viewId
  39.  
    * @param drawableId
  40.  
    * @return
  41.  
    */
  42.  
    public ViewGroupSetter childViewBgDrawable(int viewId, int drawableId)
  43.  
    {
  44.  
    mItemViewSetters.add(new ViewBackgroundDrawableSetter(viewId,
  45.  
    drawableId));
  46.  
    return this;
  47.  
    }
  48.  
     
  49.  
    /**
  50.  
    * 设置文本颜色,因此View的类型必须为TextView或者其子类
  51.  
    *
  52.  
    * @param viewId
  53.  
    * @param colorId
  54.  
    * @return
  55.  
    */
  56.  
    public ViewGroupSetter childViewTextColor(int viewId, int colorId)
  57.  
    {
  58.  
    mItemViewSetters.add(new TextColorSetter(viewId, colorId));
  59.  
    return this;
  60.  
    }
  61.  
     
  62.  
    @Override
  63.  
    public void setValue(Theme newTheme, int themeId)
  64.  
    {
  65.  
    mView.setBackgroundColor(getColor(newTheme));
  66.  
    // 清空AbsListView的元素
  67.  
    clearListViewRecyclerBin(mView);
  68.  
    // 清空RecyclerView
  69.  
    clearRecyclerViewRecyclerBin(mView);
  70.  
    // 修改所有子元素的相关属性
  71.  
    changeChildenAttrs((ViewGroup) mView, newTheme, themeId);
  72.  
    }
  73.  
     
  74.  
    /**
  75.  
    * @param viewId
  76.  
    * @return
  77.  
    */
  78.  
    private View findViewById(View rootView, int viewId)
  79.  
    {
  80.  
    View targetView = rootView.findViewById(viewId);
  81.  
    return targetView;
  82.  
    }
  83.  
     
  84.  
    /**
  85.  
    * 修改子视图的对应属性
  86.  
    *
  87.  
    * @param viewGroup
  88.  
    * @param newTheme
  89.  
    * @param themeId
  90.  
    */
  91.  
    private void changeChildenAttrs(ViewGroup viewGroup, Theme newTheme, int themeId)
  92.  
    {
  93.  
    int childCount = viewGroup.getChildCount();
  94.  
    //递归用法
  95.  
    for (int i = 0; i < childCount; i )
  96.  
    {
  97.  
    View childView = viewGroup.getChildAt(i);
  98.  
    // 深度遍历
  99.  
    if (childView instanceof ViewGroup)
  100.  
    {
  101.  
    changeChildenAttrs((ViewGroup) childView, newTheme, themeId);
  102.  
    }
  103.  
     
  104.  
    // 遍历子元素与要修改的属性,如果相同那么则修改子View的属性
  105.  
    for (ViewSetter setter : mItemViewSetters)
  106.  
    {
  107.  
    // 每次都要从ViewGroup中查找数据
  108.  
    setter.mView = findViewById(viewGroup, setter.mViewId);
  109.  
     
  110.  
    //因为viewgroup中的所有的id都添加进来了,而且每一个id都是独一无二的,
  111.  
    // 所以会有一个判断,当childView的id和setter中的id相等时,才会修改属性
  112.  
    if (childView.getId() == setter.getViewId())
  113.  
    {
  114.  
    setter.setValue(newTheme, themeId);
  115.  
    }
  116.  
    }
  117.  
    }
  118.  
    }
  119.  
     
  120.  
    private void clearListViewRecyclerBin(View rootView)
  121.  
    {
  122.  
    if (rootView instanceof AbsListView)
  123.  
    {
  124.  
    try
  125.  
    {
  126.  
    @SuppressLint("SoonBlockedPrivateApi")
  127.  
    Field localField = AbsListView.class.getDeclaredField("mRecycler");
  128.  
    localField.setAccessible(true);
  129.  
    Method localMethod = Class.forName(
  130.  
    "android.widget.AbsListView$RecycleBin")
  131.  
    .getDeclaredMethod("clear", new Class[0]);
  132.  
    localMethod.setAccessible(true);
  133.  
    localMethod.invoke(localField.get(rootView), new Object[0]);
  134.  
    Log.e("", "### 清空AbsListView的RecycerBin ");
  135.  
    } catch (NoSuchFieldException e1)
  136.  
    {
  137.  
    e1.printStackTrace();
  138.  
    } catch (ClassNotFoundException e2)
  139.  
    {
  140.  
    e2.printStackTrace();
  141.  
    } catch (NoSuchMethodException e3)
  142.  
    {
  143.  
    e3.printStackTrace();
  144.  
    } catch (IllegalAccessException e4)
  145.  
    {
  146.  
    e4.printStackTrace();
  147.  
    } catch (InvocationTargetException e5)
  148.  
    {
  149.  
    e5.printStackTrace();
  150.  
    }
  151.  
    }
  152.  
    }
  153.  
     
  154.  
    protected void clearRecyclerViewRecyclerBin(View rootView)
  155.  
    {
  156.  
    if (rootView instanceof RecyclerView)
  157.  
    {
  158.  
    try
  159.  
    {
  160.  
    Field localField = RecyclerView.class
  161.  
    .getDeclaredField("mRecycler");
  162.  
    localField.setAccessible(true);
  163.  
    Method localMethod = Class.forName(
  164.  
    "android.support.v7.widget.RecyclerView$Recycler")
  165.  
    .getDeclaredMethod("clear", new Class[0]);
  166.  
    localMethod.setAccessible(true);
  167.  
    localMethod.invoke(localField.get(rootView), new Object[0]);
  168.  
    Log.e("", "### 清空RecyclerView的Recycer ");
  169.  
    rootView.invalidate();
  170.  
    ((RecyclerView) rootView).getRecycledViewPool().clear();
  171.  
    } catch (NoSuchFieldException e1)
  172.  
    {
  173.  
    e1.printStackTrace();
  174.  
    } catch (ClassNotFoundException e2)
  175.  
    {
  176.  
    e2.printStackTrace();
  177.  
    } catch (NoSuchMethodException e3)
  178.  
    {
  179.  
    e3.printStackTrace();
  180.  
    } catch (IllegalAccessException e4)
  181.  
    {
  182.  
    e4.printStackTrace();
  183.  
    } catch (InvocationTargetException e5)
  184.  
    {
  185.  
    e5.printStackTrace();
  186.  
    }
  187.  
    }
  188.  
    }
  189.  
    }
学新通

ViewSetter

  1.  
    public abstract class ViewSetter
  2.  
    {
  3.  
    /**
  4.  
    * 目标View
  5.  
    */
  6.  
    protected View mView;
  7.  
    /**
  8.  
    * 目标view id,有时在初始化时还未构建该视图,比如ListView的Item View中的某个控件
  9.  
    */
  10.  
    protected int mViewId;
  11.  
    /**
  12.  
    * 目标View要的特定属性id
  13.  
    */
  14.  
    protected int mAttrResId;
  15.  
     
  16.  
    public ViewSetter(View targetView, int resId)
  17.  
    {
  18.  
    mView = targetView;
  19.  
    mAttrResId = resId;
  20.  
    }
  21.  
     
  22.  
    public ViewSetter(int viewId, int resId)
  23.  
    {
  24.  
    mViewId = viewId;
  25.  
    mAttrResId = resId;
  26.  
    }
  27.  
     
  28.  
    /**
  29.  
    * @param newTheme
  30.  
    * @param themeId
  31.  
    */
  32.  
    public abstract void setValue(Theme newTheme, int themeId);
  33.  
     
  34.  
    /**
  35.  
    * 获取视图的Id
  36.  
    *
  37.  
    * @return
  38.  
    */
  39.  
    protected int getViewId()
  40.  
    {
  41.  
    return mView != null ? mView.getId() : -1;
  42.  
    }
  43.  
     
  44.  
    protected boolean isViewNotFound()
  45.  
    {
  46.  
    return mView == null;
  47.  
    }
  48.  
     
  49.  
    /**
  50.  
    * @param newTheme
  51.  
    * @return
  52.  
    */
  53.  
    protected int getColor(Theme newTheme)
  54.  
    {
  55.  
    //返回重新指定后的资源id
  56.  
    TypedValue typedValue = new TypedValue();
  57.  
    newTheme.resolveAttribute(mAttrResId, typedValue, true);
  58.  
    return typedValue.data;
  59.  
    }
  60.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfaigc
系列文章
更多 icon
同类精品
更多 icon
继续加载