android 使用Theme + attr换肤

项目有支持夜间模式的需求,开始从theme一点点摸索。据了解。很多人采用的方式是更换主题后发送通知,所有界面在接受到通知后,重新set一遍控件。这样的好处是不需要重启界面,但是代码写起来比较繁琐,且代码量很大。

首先从Theme + attr结合的方式开始,比如说在attr.xml里可以定义如下属性:

<attr name="btnColor" format="color" />
    <attr name="mainBackground" format="color" />
    <attr name="mainTextColor" format="reference|color" />
    <attr name="textString" format="string" /> 
    <attr name="textStyle" format="reference"></attr>
    <declare-styleable name="EditTextExt">
        <attr name="TextColor" format="reference|string"></attr>
    </declare-styleable><span style="font-family: Arial, Helvetica, sans-serif;">	</span>

 
然后在styles.xml里定义主题: 

<!-- 默认 -->
    <style name="MyThemeDefault" parent="@android:style/Theme">
        <item name="btnColor">#00ff00</item>
        <item name="mainBackground">#ffffff</item>
        <item name="mainTextColor">#367895</item>
        <item name="textString">默认主题</item>
        <item name="textStyle">@style/textStyle.TMyTextStyle</item>
    </style>

    <!-- 夜间 -->
    <style name="MyThemeNight" parent="@android:style/Theme">
        <item name="btnColor">#0000ff</item>
        <item name="mainBackground">#000000</item>
        <item name="mainTextColor">#555555</item>
        <item name="textString">夜间主题</item>
        <item name="textStyle">@style/textStyleNight.TMyTextStyle</item>
    </style>
我们可以在theme里定义好某个属性的颜色,然后在具体的style中使用: 

    <style name="TextColorSection">
        <item name="android:textColor">?attr/mainTextColor</item>
    </style>
设置主题有两种途径,一种是在activity里设置,一种是在fragment里设置。

在Activity中设置的方法如下,在onCreate函数里根据当前主题设置:

setTheme(R.style.MyThemeNight);
在inflate布局时要注意,使用当前activity的Context加载布局,如mContext.getLayoutInflater.inflate.不然在加载布局时会找不到设置的属性值而崩溃。

在fragment里设置主题时,在onCreateView里进行如下设置:

Context ctxWithTheme;
    	ctxWithTheme = new ContextThemeWrapper(getActivity().getApplicationContext(), R.style.MyThemeNight); 
    	mInflater = inflater.cloneInContext(ctxWithTheme); 
在加载布局时使用创建的mInflater进行加载。

个人理解,这两种方法本质上都是将theme的内容添加到当前的上下文信息context中进行加载。


在代码中可以使用如下方法,使用attr属性设置颜色。。再也不用if else啦。。

<pre name="code" class="java">TypedValue typedValue = new TypedValue();
         MainActivity.getInstance().getTheme().resolveAttribute(R.style.TextColorItemTitle, typedValue, true);
         int[] attribute = new int[] { R.attr.text_cl_title };
         TypedArray array = MainActivity.getInstance().obtainStyledAttributes(typedValue.resourceId, attribute);
         int textColor = array.getColor(0, R.color.kw_commen_color_white);
         tabView.setTextColor(textColor);
         array.recycle();


 






  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 首先,在布局文件中定义BottomSheet和RecycleView: ``` <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.bottomsheet.BottomSheetBehavior android:id="@+id/bottom_sheet" android:layout_width="match_parent" android:layout_height="wrap_content" app:behavior_hideable="true" app:behavior_peekHeight="0dp" app:layout_behavior="@string/bottom_sheet_behavior"> <LinearLayout android:id="@+id/bottom_sheet_content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </com.google.android.material.bottomsheet.BottomSheetBehavior> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` 2. 在Activity中,初始化BottomSheetBehavior和RecycleView: ``` private BottomSheetBehavior bottomSheetBehavior; private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(new MyAdapter()); bottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.bottom_sheet)); bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); // 设置地图的触摸事件监听器,用于处理拖拽事件 AMap aMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); aMap.setOnMapTouchListener(new AMap.OnMapTouchListener() { @Override public void onTouch(MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) { if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) { bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); } } } }); } ``` 3. 在MyAdapter中,实现RecyclerView的内容: ``` public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> { private List<String> data = new ArrayList<>(); public MyAdapter() { for (int i = 0; i < 20; i++) { data.add("Item " + i); } } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false); return new MyViewHolder(view); } @Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { holder.textView.setText(data.get(position)); } @Override public int getItemCount() { return data.size(); } } public class MyViewHolder extends RecyclerView.ViewHolder { public TextView textView; public MyViewHolder(@NonNull View itemView) { super(itemView); textView = itemView.findViewById(R.id.text_view); } } ``` 4. 在布局文件中定义item_layout.xml: ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/listPreferredItemHeight" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Item" android:textSize="16sp" android:textStyle="bold" /> </LinearLayout> ``` 5. 运行程序,拖动地图时,BottomSheet会自动展开,显示RecyclerView的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值