先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7
深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
如果你需要这些资料,可以添加V获取:vip204888 (备注Android)
正文
1.如何自定义控件?
系统是怎么做的?以RelativeLayout为例:
第一步:在atts.xml中配置需要的属性
第二步:重写控件,以满足自己的要求
第三步:在 xml 文件中使用控件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“.MainActivity”>
1.下面我们可以仿照系统的步骤来自定义我们的控件:
效果预览:
此控件由左右两个Button和中间一个TextView组合而成。
第一步:创建 atts.xml, 并配置需要的属性
<?xml version="1.0" encoding="utf-8"?>第二步:实现自己的 View
package com.lzw.topbar;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
/**
-
顶部导航栏控件
-
这是一个组合控件,由左右两个Button和一个TextView组成
*/
public class TopBar extends RelativeLayout {
// 步骤2:定义需要使用的控件
private Button leftButton , rightButton;
private TextView tvTitle;
// 步骤3:声明这些控件所需要使用的属性,即之前在 atts.xml 中定义的属性
// 左 Button 属性
private String leftText;
private int leftTextColor;
private Drawable leftBackground;
// 右 Button 属性
private String rightText;
private int rightTextColor;
private Drawable rightBackground;
// 中间 TextView 属性
private String title;
private int titleTextColor;
private float titleTextSize;
private LayoutParams leftParams,rightParams,titleParams;
private topbarClickListener listener;
public interface topbarClickListener{
void leftClick();
void rightClick();
}
public void setOnTopBarClickListener(topbarClickListener listener){
this.listener = listener;
}
// 不需要自定义属性的构造方法
public TopBar(Context context) {
super(context);
}
/**
-
步骤1:添加构造方法
-
需要自定义属性就使用这一个构造方法,使用atts参数
-
@param context
-
@param attrs
*/
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public TopBar(final Context context, AttributeSet attrs) {
super(context, attrs);
// 步骤4:给声明好的属性赋值,以将属性和控件关联
// 4.1 通过TypedArray存储从xml文件中获取到的自定义属性的值,并赋给相应的变量
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
leftText = ta.getString(R.styleable.TopBar_leftText);
leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor,0);
leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);
rightText = ta.getString(R.styleable.TopBar_rightText);
rightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor,0);
rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);
title = ta.getString(R.styleable.TopBar_title);
titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize,0);
titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor,0);
// 回收。1.避免浪费资源 2.避免由于缓存而引起的错误
ta.recycle();
// 4.2 实例化控件
leftButton = new Button(context);
rightButton = new Button(context);
tvTitle = new Button(context);
// 4.3 将属性值和控件关联
leftButton.setText(leftText);
leftButton.setTextColor(leftTextColor);
leftButton.setBackground(leftBackground);
rightButton.setText(rightText);
rightButton.setTextColor(rightTextColor);
rightButton.setBackground(rightBackground);
tvTitle.setText(title);
tvTitle.setTextSize(titleTextSize);
tvTitle.setTextColor(titleTextColor);
tvTitle.setGravity(Gravity.CENTER);
tvTitle.setBackgroundColor(Color.argb(0,0,0,0));
setBackgroundColor(0xFFF59563);
// 步骤5:将控件放到layout中,通过LayoutParams控制摆放方式
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
addView(leftButton, leftParams);
rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注Android)
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
tParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
[外链图片转存中…(img-4OWbbkKs-1713660542859)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-c92crQ9d-1713660542859)]
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!