Android Studio多Module应用

1. 创建Android库

创建Module,打开菜单【File】 -> 【New】 -> 【New Module…】
在这里插入图片描述
选择Android Library
在这里插入图片描述
输入Module namePackage name
在这里插入图片描述
在app的Dependencies中添加依赖库
在这里插入图片描述

2. 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库

打开菜单 【File】 -> 【New】 -> 【Import Module…】,在Dependencies中添加依赖库
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值