Android Studio多Module应用

来源:互联网 发布:php命名空间找不到类 编辑:程序博客网 时间:2024/06/16 22:28

Android Studio多Module应用

1、创建Android库

(1) 创建Module
File -> 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
原创粉丝点击