自定义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. js中replace未定义_js中replace的用法
  2. Geany整体注释和取消注释快捷键
  3. docker之centos7安装docker
  4. C#:ref和out的联系及区别。
  5. ffmpeg添加到环境变量_Windows + MSVC环境编译ffmpeg
  6. 性能测试LoadRunner_Monitors
  7. JAVA IOC及代理模式
  8. Project Euler 628: Open chess positions(公式)
  9. Django 简易实现用户保持登录状态2月
  10. 厉害了!「00后缩写黑话翻译器」登上GitHub热榜,中年网民终于能看懂年轻人的awsl...
  11. 摄氏温度转化为华氏温度代码
  12. vs2013设置winp#cap开发环境
  13. 音视频开发---音视频同步算法
  14. 写数据分析报告,建议部分憋到脸红,咋整?
  15. Python数据可视化,Pyecharts库,外圆环内饼图制作
  16. 商业模式是利益相关者的交易结构
  17. 【STM32】:RCC时钟系统
  18. 神经网络应用较多的算法,图卷积神经网络应用
  19. 【DCT】基于simulink的dual clutch Transmission双离合器变速器系统仿真系统详细解析
  20. 腾讯自选股任务 青龙脚本

热门文章

  1. 外汇天眼:国庆特辑⑤·中国外汇交易商的交易模式
  2. Windows客户端开发简介(二)
  3. python语言不支持面向对象_Python 面向对象(初级篇)
  4. PLC是一种专门为在工业环境下应用而设计
  5. 如何获取网页图标(favicon.ico无效的一种方法)
  6. 瑞萨IDE:CS+ for CC新建工程配置方法
  7. 名词解释第七十五讲:溢价
  8. SAP搜索帮助内部错误:表格格式
  9. Php禁止外部盗链nginx,Nginx防止盗链
  10. 高中生计算机报纸word,电子板报(word)_高中生学习报.doc