自定义view嵌套使用,简单操作。

一:简单的自定义view(relativeLayout)

public class ViewRelativeLayout extends RelativeLayout {

public ViewRelativeLayout(Context context) {

super(context);

}

public ViewRelativeLayout(Context context, AttributeSet attrs) {

super(context, attrs);

initView();

}

public ViewRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

initView();

}

private void initView() {

//R.layout.demo_01

View view = LayoutInflater.from( getContext()).inflate(R.layout.demo_01, this, false);

addView(view);

}

}

R.layout.demo_01

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@color/design_default_color_primary">

android:id="@+id/demo_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Demo" />

一:嵌套使用(recyclerView)

1:先创建RecyclerView相关布局文件

RecyclerView

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@color/design_default_color_primary">

android:id="@+id/rel_1"

android:background="@color/design_default_color_primary_dark"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_centerInParent="true" />

RecyclerView——item

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/item_text"

android:textSize="50dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

2:Adapter

public class DemoAdapter extends RecyclerView.Adapter {

private List list;

private Context context;

public DemoAdapter(Context context, List list) {

this.context = context;

this.list = list;

}

@NonNull

@Override

public DemoAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view = LayoutInflater.from(context).inflate(R.layout.item_adapter, parent, false);

return new MyViewHolder(view);

}

@Override

public void onBindViewHolder(@NonNull DemoAdapter.MyViewHolder holder, int position) {

MyViewHolder viewHolder = (MyViewHolder) holder;

viewHolder.title.setText(list.get(position).toString() );

}

@Override

public int getItemCount() {

return list.size();

}

class MyViewHolder extends RecyclerView.ViewHolder {

public TextView title;

public TextView content;

public MyViewHolder(@NonNull View itemView) {

super(itemView);

title = itemView.findViewById(R.id.item_text);

title.setTextColor(Color.parseColor("#F0F0F0"));

}

}

3:嵌套使用

public class Demo1 extends RelativeLayout {

public Demo1(Context context) {

super(context);

}

public Demo1(Context context, AttributeSet attrs) {

super(context, attrs);

initview(context);

}

public Demo1(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

private List list;

TextView textView;

RecyclerView recyclerView;

private void initview(Context context) {

View view = LayoutInflater.from( getContext()).inflate(R.layout.demo_01, this, false);

recyclerView = view.findViewById(R.id.rel_1);

init(context);

addView(view);

}

DemoAdapter demoAdapter;

private void init(Context context) {

list = new ArrayList<>();

for (int i = 0; i < 12; i++) {

if (i == 10) {

list.add(0);

} else {

list.add(i + 1);

}

}

demoAdapter = new DemoAdapter(context, list);

recyclerView.setLayoutManager(new GridLayoutManager(context, 4));

recyclerView.setAdapter(demoAdapter);

}

}

布局上使用

android:id="@+id/demoview"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

Android小白一枚,脑子不够,笔记来凑,

日常更新小白知识点(自己的知识点,啊哈哈),

写个小故事吧:

没啥故事可说的了,以后编好了再讲故事,每天记录一下,加油,我会更棒,对生活充满希望!不要想房贷车贷保险结婚生子等等等等。

android 继承relativelayout,Android自定义View(RelativeLayout),并嵌套(recyclerView)相关推荐

  1. Android动画特效之自定义View

      Android动画特效之Animator属性动画实现_Angel-杭州的博客-CSDN博客   我在百忙之中抽出宝贵时间来实现Android动画特效,也就是Android Animator动画效果 ...

  2. Android实现雪花特效自定义view

    一.前言 这个冬天,老家一直没有下雨, 正好圣诞节,就想着制作一个下雪的特效. 圣诞祝福:平安夜,舞翩阡.雪花飘,飞满天.心与心,永相伴. 圣诞节是传统的宗教节日,对于基 督徒,那是庆祝耶稣的诞生,纪 ...

  3. Android 气泡动画(自定义View类)

    Android 气泡动画(自定义View类) 一.前言 二.代码 1. 随机移动的气泡 2.热水气泡 一.前言 最近有需求制作一个水壶的气泡动画,首先在网上查找了一番,找到了一个文章. https:/ ...

  4. 【Android 应用开发】自定义View 和 ViewGroup

    一. 自定义View介绍 自定义View时, 继承View基类, 并实现其中的一些方法. (1) ~ (2) 方法与构造相关 (3) ~ (5) 方法与组件大小位置相关 (6) ~ (9) 方法与触摸 ...

  5. Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)

    转载请注明地址:http://blog.csdn.net/xiaanming/article/details/10298163 很多的时候,系统自带的View满足不了我们功能的需求,那么我们就需要自己 ...

  6. Android自定义控件面试题,自定义View面试总结

    本着针对面试,不负责任的态度,写下<面试总结>系列.本系列记录面试过程中各个知识点,而不是入门系列,如果有不懂的自行学习. 自定义View三种方式,组合现有控件,继承现有控件,继承View ...

  7. android卡包动画,自定义View实现银行卡卡包动画效果

    本来不想自己造轮子的,但奈何没找动相应效果的轮子,所以只能自己写了,其实还是白嫖来的轻松,哈哈 先看效果 这个是完成的效果,还可以吧!关键也不难一个自定义View搞定 先说一下思路,继承一个Relat ...

  8. Android属性动画与自定义View——实现vivo x6更新系统的动画效果

    晚上好,现在是凌晨两点半,然后我还在写代码.电脑里播放着<凌晨两点半>,晚上写代码,脑子更清醒,思路更清晰. 今天聊聊属性动画和自定义View搭配使用,前面都讲到自定义View和属性动画, ...

  9. Android Paint应用之自定义View实现进度条控件

    在上一篇文章<Android神笔之Paint>学习了Paint的基本用法,但是具体的应用我们还没有实践过.从标题中可知,本文是带领读者使用Paint,自定义一个进度条控件. 上图就是本文要 ...

最新文章

  1. mongo报错:not authorized on bb to execute command { create: \“xxx\“...}
  2. RabbitMQ 中 7 种消息队列
  3. MySQL中exists与in的使用
  4. git使用的一些常用命令
  5. 用python设计一个管理系统思路_Python大佬一个月打造的Python系统学习流程图!
  6. 安装cloudermanager时出现Acquiring installation lock问题(图文详解)
  7. SAP Spartacus split view右边视图的overflow属性三种不同的值
  8. 开源视觉salm算法介绍
  9. t–sql pl–sql_SQL串联正确完成–第1部分–可疑做法
  10. Android语音识别开发详解(基于讯飞语音SDK)
  11. 基于SpringBoot的在线音乐播放系统
  12. 微信公众平台开发(五) 天气预报功能开发
  13. 一图读懂3GPP R16(附思维导图下载)
  14. 计算机类603高数,拟录取ING ,谈谈603高等数学
  15. 离散时间傅里叶变换Matlab实现
  16. 华为手机如何设置主页面_华为手机怎么进行桌面管理?正确管理华为手机桌面图标及屏幕的方法...
  17. linux下查看进程占用网络,linux怎么查看进程占用端口
  18. PyTorch基础:神经网络工具箱torch.nn(优化器nn.optim)
  19. Stimulsoft Reports如何进行数据连接
  20. 我的世界服务器货币充值系统,《我的世界》中国版正式开启了充值功能

热门文章

  1. 让我来教你清除系统中的垃圾(转)
  2. OSChina 周二乱弹 ——告诉你们活到一百岁的秘密!
  3. 智能慧工厂虚拟仿真管理系统软件
  4. 2022-2028全球及中国豪华乙烯基地板(LVT)行业研究及十四五规划分析报告
  5. vim配置python开发环境_Win10系统下安装编辑器之神(The God of Editor)Vim并且构建Python生态开发环境(2020年最新攻略)...
  6. DATA-轉載【数据科学】教你成为数据科学“大咖”!
  7. Python+Opencv图像处理--基于OTSU+凸包检测的粘连大米分割
  8. 蓝桥杯趣味算式(递归)
  9. js引擎执行js代码的过程
  10. 一个真实而又令人震撼的故事