简介
最近终于学完了了郭书神的《第一话代码》,决定自己着手写个项目,在开发中遇到了几个问题:
- 类似的布局会存在大量相同代码。
- java文件的某些继承自父类方法必须重写的方法中含有过多代码。
- 过多的图片和布局文件全放在一个文件夹下难以区分也加大寻找资源的难度。
我把方法总结为重复代码的简略(layout文件)和项目文件的分类排放。
(因为第二点暂时没想好解决方法)
重复代码的简略
style
- 适应场景
- 多个控件(控件类型可以不同)含有相同的属性
- 举例
- 我有几个大小相同但颜色有区别的button
- 使用方法
- 打开app\src\main\res\values\styles.xml文件新写入一个style,如下:
这样就添加好了一个新的style,我们可以在布局文件中使用它了。<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <!-- 上面是原有代码,添加如下代码 --> <style name="MyButton"> <item name="android:layout_width">100dp</item> <item name="android:layout_height">50dp</item> </style> </resources>
- 在你的布局文件中添加以下代码
这样就省略了相同的代码,当然你还可以在style中添加更多属性来简化你的页面布局<Button android:id="@+id/button1" style="@style/MyToolBar" android:background="#000000"/> <Button android:id="@+id/button2" style="@style/MyToolBar" android:background="#ffffff"/>
- 注意事项
- item name属性对应xml文件“=”前面的部分,尖括号中的内容则对应xml文件引号中的内容。
- 对app这样的命名空间和高版本sdk可以省略前面的命名空间,控件的属性依然有效
- 可以用parent继承其他style的全部属性,如有不同的属性则可通过重写覆盖。
- 适用于多种多样的控件含有相同属性。
include
- 适应场景
- 多个页面含有相同的子布局
- 举例
- 如qq的actionbar
- 使用方法
- 打开app\src\main\res\layout下新建文件tool_bar.xml,这里我们用toolbar代替actionbar的效果。
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/coor_layout" android:layout_width="match_parent" android:layout_height="wrap_content" > <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </com.google.android.material.appbar.AppBarLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>
- 其实通过上面的操作我们已经自定义了一个控件,现在只用在你的主布局中用出来就行了,在你的主布局中添加如下代码
这样添加了代码的布局中就都会存在toolbar啦。图中不同的text和菜单的创建可以留到相应的活动中实现。<include android:layout_height="wrap_content" android:layout_width="match_parent" layout="@layout/tool_bar"/>
- 注意事项
- 自定义的控件不用命名,findViewById也能分辨出不同xml中的同名控件。
- 适用于同一个控件却在不同页面有不同的事件(如点击事件)。
碎片
- 适应场景
- 多个页面存在布局和事件都相同的控件
- 举例
- 如qq的导航栏
- 使用方法
- 相当于建立一个完整的活动-页面体系,于是我们要分别建立这个体系的活动和页面
- 新建area.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="match_parent" android:background="#fff" android:orientation="vertical"> <Button android:id="@+id/back_button" android:layout_width="25dp" android:layout_height="25dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:background="@drawable/ic_back" /> <TextView android:id="@+id/title_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textColor="#fff" android:textSize="20sp" /> </LinearLayout>
- 再新建AFragment.class
public class AFragment extends Fragment { private Button button; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.area, container, false); button = view.findViewById(R.id.back_button); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity(),"Clicked",Toast.LENGTH_SHORT).show(); } }); } }
- 然后你只要在你的布局中加上这个碎片便可以使用了。
<fragment android:id="@+id/one_fragment" android:name="com.android.Main.AFragment" android:layout_width="match_parent" android:layout_height="match_parent" />
- 注意事项
- 碎片适用于页面活动完全一样的控件,如qq的底部导航栏和侧边菜单栏
- 碎片有自己的生命周期区别于活动和include
详见Android 碎片。