做网站服务器一年多少钱,哪个平台查企业免费,深圳企业vi设计公司,创办网站需要什么安卓开发自定义时间日期显示组件
问题背景
实现时间和日期显示#xff0c;左对齐和对齐两种效果#xff0c;如下图所示#xff1a;
问题分析
自定义view实现一般思路#xff1a; #xff08;1#xff09;自定义一个View #xff08;2#xff09;编写values/attrs.…安卓开发自定义时间日期显示组件
问题背景
实现时间和日期显示左对齐和对齐两种效果如下图所示
问题分析
自定义view实现一般思路 1自定义一个View 2编写values/attrs.xml在其中编写styleable和item等标签元素 3在布局文件中View使用自定义的属性 4在View的构造方法中通过TypedArray获取
问题解决
话不多说直接上代码 1编写values/attrs.xml组件定义left属性
?xml version1.0 encodingutf-8?
resourcesdeclare-styleable nameTimeClockViewattr nameleft formatboolean//declare-styleable
/resources2自定义View代码如下
public class TimeClockView extends LinearLayout {boolean isLeft true;public TimeClockView(Context context) {super(context);initView(context);}private void initView(Context context) {if (isLeft) {LayoutInflater.from(context).inflate(R.layout.layout_time_date,this);} else {LayoutInflater.from(context).inflate(R.layout.layout_time_date1,this);}}public TimeClockView(Context context, AttributeSet attrs) {super(context, attrs);initTypeValue(context,attrs);initView(context);}public void initTypeValue(Context context ,AttributeSet attrs){TypedArray a context.obtainStyledAttributes(attrs, R.styleable.TimeClockView);isLeft a.getBoolean(R.styleable.TimeClockView_left, true);a.recycle();}
}3自定义view对应的布局文件如下 左对齐
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:orientationverticalandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentTextClockandroid:idid/timeandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:format12Hourhh:mmandroid:format24HourHH:mmandroid:textSize40px /TextClockandroid:idid/dateandroid:layout_belowid/timeandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:format12HourMM月dd日 Eandroid:format24HourMM月dd日 Eandroid:textSize20px /
/LinearLayout右对齐
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:gravityendandroid:orientationverticalandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentTextClockandroid:idid/timeandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:format12Hourhh:mmandroid:format24HourHH:mmandroid:textSize40px /TextClockandroid:idid/dateandroid:layout_belowid/timeandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:format12HourMM月dd日 Eandroid:format24HourMM月dd日 Eandroid:textSize20px /
/LinearLayout4在页面布局中使用自定义的view
?xml version1.0 encodingutf-8?
androidx.constraintlayout.widget.ConstraintLayoutxmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivitycom.baorant.mytestnew.view.TimeClockViewandroid:layout_marginLeft90pxandroid:layout_marginTop70pxapp:layout_constraintLeft_toLeftOfparentapp:layout_constraintTop_toTopOfparentandroid:layout_widthwrap_contentandroid:layout_heightwrap_content /com.baorant.mytestnew.view.TimeClockViewandroid:layout_marginRight90pxandroid:layout_marginTop70pxapp:leftfalseapp:layout_constraintRight_toRightOfparentapp:layout_constraintTop_toTopOfparentandroid:layout_widthwrap_contentandroid:layout_heightwrap_content//androidx.constraintlayout.widget.ConstraintLayout