Android Studio多Module应用
来源:互联网 发布:php命名空间找不到类 编辑:程序博客网 时间:2024/06/16 22:28
Android Studio多Module应用
1、创建Android库
(1) 创建ModuleFile -> New -> New Module...
(2) 选择Android Library
(3) 输入module名称和包名
(4) 在app的Dependencies中添加依赖库
2、Android库DemoLib
在DemoLib中有一个工具类TimeFormat和自定义界面类CustomView。TimeFormat类将日期格式化。
public class TimeFormat { public static String timeToYearMinute(long millisecond) { return milliToFormat(millisecond, "yyyy-MM-dd HH:mm"); } public static String milliToFormat(long millisecond, String format) { if (millisecond == 0) return ""; Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault()); date.setTime(millisecond); return sdf.format(date); }}CustomView类继承RelativeLayout类,包含两个TextView。
public class CustomView extends RelativeLayout { private TextView mTvTitleTop, mTvTitleBottom; public CustomView(Context context) { this(context, null); } public CustomView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); View view = inflate(context, R.layout.layout_custom_view, this); mTvTitleTop = (TextView) view.findViewById(R.id.tv_title_top); mTvTitleBottom = (TextView) view.findViewById(R.id.tv_title_bottom); } public void setTitleTop(CharSequence title) { mTvTitleTop.setText(title); } public void setTitleBottom(CharSequence title) { mTvTitleBottom.setText(title); }}布局文件layout_custom_view.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_title_top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/font_size_xhdpi_16" android:layout_alignParentLeft="true" android:layout_marginLeft="@dimen/margin_xdpi_15" android:layout_centerVertical="true" /> <TextView android:id="@+id/tv_title_bottom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/font_size_xhdpi_12" android:layout_alignParentRight="true" android:layout_marginRight="@dimen/margin_xdpi_15" android:layout_centerVertical="true" /> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#ff000000" android:layout_alignParentBottom="true"/></RelativeLayout>资源文件dimens.xml
<?xml version="1.0" encoding="utf-8"?><resources> <dimen name="margin_xdpi_15">15dp</dimen> <dimen name="font_size_xhdpi_16">16sp</dimen> <dimen name="font_size_xhdpi_12">12sp</dimen></resources>
3、使用Android库
布局文件layout_main.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"> <com.blog.demo.lib.ui.CustomView android:id="@+id/custom_view" android:layout_width="match_parent" android:layout_height="50dp" /></LinearLayout>自定义activity
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_main); CustomView customView = (CustomView) findViewById(R.id.custom_view); customView.setTitleTop("时间"); Calendar calendar = Calendar.getInstance(); customView.setTitleBottom(TimeFormat.timeToYearMinute(calendar.getTimeInMillis())); }}
显示如下
4、导入一个已有的android库
(1) File -> New -> Import Module...(2) 在app的Dependencies中添加依赖库
0 0
- Android Studio多Module应用
- Android Studio删除Module
- Android Studio 删除 Module
- Android Studio 删除Module
- Android Studio删除module
- Android Studio删除module
- Android Studio 删除 Module
- android studio删除module
- Android Studio删除module
- Android Studio 导入module
- Android Studio删除Module
- Android Studio 删除module
- android studio导入Module
- Android studio 导入module
- Android Studio 导入Module
- Android Studio删除module
- android studio java module
- Android Studio如何删除module
- Java学习之类与对象
- 设计模式 之抽象工厂
- 同时使用两种方式启动服务
- 最全正则表达式总结:验证QQ号、手机号、Email、中文、邮编、身份证、IP地址等
- swift3.0中自定义的相机,不好别喷,给点提示,多多指教
- Android Studio多Module应用
- spring四种依赖注入方式
- C++作业3
- npm必杀技
- SearchView+RecyclerView+GreenDao的搜索功能实现(2)
- VS使用技巧——如何自动在新建项目中添加语句
- Linux(centos6.5)下安装OpenSSL 安装图文详解
- Spark 2.0介绍:Dataset介绍和使用
- 倒计时案例 实现