AS-项目文件的规范化管理

简介

最近终于学完了了郭书神的《第一话代码》,决定自己着手写个项目,在开发中遇到了几个问题:

  • 类似的布局会存在大量相同代码。
  • java文件的某些继承自父类方法必须重写的方法中含有过多代码。
  • 过多的图片和布局文件全放在一个文件夹下难以区分也加大寻找资源的难度。

我把方法总结为重复代码的简略(layout文件)项目文件的分类排放
(因为第二点暂时没想好解决方法)

重复代码的简略

style

  • 适应场景
    • 多个控件(控件类型可以不同)含有相同的属性
  • 举例
    • 我有几个大小相同但颜色有区别的button
  • 使用方法
    • 打开app\src\main\res\values\styles.xml文件新写入一个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"/>
      
      这样就省略了相同的代码,当然你还可以在style中添加更多属性来简化你的页面布局
  • 注意事项
    • item name属性对应xml文件“=”前面的部分,尖括号中的内容则对应xml文件引号中的内容。
    • 对app这样的命名空间和高版本sdk可以省略前面的命名空间,控件的属性依然有效
    • 可以用parent继承其他style的全部属性,如有不同的属性则可通过重写覆盖。
    • 适用于多种多样的控件含有相同属性。

详见android中用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>
    
    • 其实通过上面的操作我们已经自定义了一个控件,现在只用在你的主布局中用出来就行了,在你的主布局中添加如下代码
      <include
      	  android:layout_height="wrap_content"
      	  android:layout_width="match_parent"
         	  layout="@layout/tool_bar"/>
      
      这样添加了代码的布局中就都会存在toolbar啦。图中不同的text和菜单的创建可以留到相应的活动中实现。
  • 注意事项
    • 自定义的控件不用命名,findViewById也能分辨出不同xml中的同名控件。
    • 适用于同一个控件却在不同页面有不同的事件(如点击事件)。

详见android中include标签使用详解

碎片

  • 适应场景
    • 多个页面存在布局和事件都相同的控件
  • 举例
    • 如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 碎片

项目文件的分类排放

布局文件

  • 吐槽
    • 前面重复代码简略的应用使xml文件不止作为活动的基础了,有各种各样的应用这样又使layout文件夹内的xml显得纷繁复杂,有强迫症同学的噩梦啊,但是在layout和res文件夹下再建文件夹却无法正常使用xml文件。于是我就在网上找到了方法。
  • 方法

图片资源

  • 方法
    • 和布局文件方法一样
  • 建议
    • 内部图片放在drawable
    • 控件图标放在mipmap
发布了6 篇原创文章 · 获赞 0 · 访问量 1270

猜你喜欢

转载自blog.csdn.net/yiself/article/details/104271740
今日推荐