Android实现Z轴布局效果
作者:杨天睿 时间:2021-11-19 05:33:56
如果需要在布局中创造一个层叠的概念,那么使用Android系统中的ViewGroup是不够的,但是可以通过改变ViewGroup的绘制顺序实现
代码下载
继承自FrameLayout
FrameLayout已经帮我们实现了子View的measure和layout过程,我们只需在它的基础上改变绘制顺序即可
自定义LayoutParams
layoutParams的作用是向父布局请求布局参数(MeasureSpec),这个参数会在View inflate时添加到布局中,我们如果使用LayoutParams将会得到很大的方便
// 这里继承FrameLayout的LayoutParams即可
public static class LayoutParams extends FrameLayout.LayoutParams {
public final static int DEFAULT_ZORDER = 1;
public int zOrder;
public LayoutParams(@NonNull Context c, @Nullable AttributeSet attrs) {
super(c, attrs);
TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ZOrderLayout);
zOrder = a.getInt(R.styleable.ZOrderLayout_layout_zorder, DEFAULT_ZORDER);
a.recycle();
}
}
我们自定义个Attribute,那么就可以在XML中进行使用了
<declare-styleable name="ZOrderLayout">
<attr name="layout_zorder" format="integer"/>
</declare-styleable>
这样我们的View就可以这么使用
<!--layout_zorder 表示该View在第1层-->
<tianrui.viewgroup.MyTextView
android:text="0"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@android:color/holo_red_light"
app:layout_zorder="1"/>
<!--layout_zorder=2 表示该View在第2层-->
<tianrui.viewgroup.MyTextView
android:text="1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:background="@android:color/holo_blue_light"
app:layout_zorder="2"/>
同时需要重写ViewGroup的generateLayoutParams(),让它生成我们的LayoutParams
初始化绘制顺序
在所有的子View加载完成后初始化需要绘制的顺序(根据我们的ZorderLayoutParams)
@Override
protected void onFinishInflate() {
super.onFinishInflate();
initialZOrder();
}
private void initialZOrder() {
final int childCount = getChildCount();
View view;
ZOrderLayout.LayoutParams params;
for (int i = 0; i < childCount; i++) {
view = getChildAt(i);
params = (LayoutParams) view.getLayoutParams();
Pair<View, Integer> pair = new Pair<>(view, params.zOrder);
list.add(pair);
}
// 根据Zorder属性,进行排序
Collections.sort(list, new Comparator<Pair<View, Integer>>() {
@Override
public int compare(Pair<View, Integer> o1, Pair<View, Integer> o2) {
return o1.second - o2.second;
}
});
}
获取所有的子View,然后根据他们的ZOrder进行排序,onFinishInflate()会在装载完所有的子View后进行回调
改变View的绘制顺序
这里使用排好序的View绘制顺序就可以了, 记得调用setChildrenDrawingOrderEnabled(true);
@Override
protected int getChildDrawingOrder(int childCount, int i) {
return indexOfChild(list.get(i).first);
}
Demo演示
<?xml version="1.0" encoding="utf-8"?>
<tianrui.viewgroup.view.ZOrderLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<tianrui.viewgroup.MyTextView
android:text="0"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@android:color/holo_red_light"
app:layout_zorder="1"/>
<tianrui.viewgroup.MyTextView
android:text="1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:background="@android:color/holo_blue_light"
app:layout_zorder="2"/>
<tianrui.viewgroup.MyTextView
android:text="2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="40dp"
android:background="@android:color/holo_orange_light"
app:layout_zorder="3"/>
<tianrui.viewgroup.MyTextView
android:text="3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="60dp"
android:background="@android:color/holo_green_light"
app:layout_zorder="2"/>
<tianrui.viewgroup.MyTextView
android:text="4"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="80dp"
android:background="@android:color/holo_purple"
app:layout_zorder="1"/>
</tianrui.viewgroup.view.ZOrderLayout>
可以看出这个布局是中间的zorder最高,表示中间的会压在两边的上边,而最左(右)的绘制层级(zorder)为1, 表示会绘制在最下面
完整代码
public class ZOrderLayout extends FrameLayout {
private List<Pair<View, Integer>> list = new ArrayList<>();
public ZOrderLayout(@NonNull Context context) {
this(context, null);
}
public ZOrderLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public ZOrderLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
setChildrenDrawingOrderEnabled(true);
}
@Override
protected int getChildDrawingOrder(int childCount, int i) {
return indexOfChild(list.get(i).first);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
initialZOrder();
}
private void initialZOrder() {
final int childCount = getChildCount();
View view;
ZOrderLayout.LayoutParams params;
for (int i = 0; i < childCount; i++) {
view = getChildAt(i);
params = (LayoutParams) view.getLayoutParams();
Pair<View, Integer> pair = new Pair<>(view, params.zOrder);
list.add(pair);
}
Collections.sort(list, new Comparator<Pair<View, Integer>>() {
@Override
public int compare(Pair<View, Integer> o1, Pair<View, Integer> o2) {
return o1.second - o2.second;
}
});
}
/**
* 在解析xml时,会解析每个跟布局的LayoutParams
*/
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
public static class LayoutParams extends FrameLayout.LayoutParams {
public final static int DEFAULT_ZORDER = 1;
public int zOrder;
public LayoutParams(@NonNull Context c, @Nullable AttributeSet attrs) {
super(c, attrs);
TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ZOrderLayout);
zOrder = a.getInt(R.styleable.ZOrderLayout_layout_zorder, DEFAULT_ZORDER);
a.recycle();
}
}
}
来源:https://blog.csdn.net/y874961524/article/details/77918116
标签:Android,布局
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#学习笔记整理_深入剖析构造函数、析构函数
2022-10-26 18:37:20
Java多线程案例之单例模式懒汉+饿汉+枚举
2021-11-07 05:18:01
![](https://img.aspxhome.com/file/2023/0/77130_0s.png)
JDBC连接MySQL并实现模糊查询
2021-07-06 01:42:28
一文给你通俗易懂的讲解Java异常
2021-12-20 14:40:56
![](https://img.aspxhome.com/file/2023/4/66694_0s.png)
解决SpringMVC、tomcat、Intellij idea、ajax中文乱码问题
2023-10-20 10:16:23
![](https://img.aspxhome.com/file/2023/5/72625_0s.jpg)
java 静态代理 动态代理深入学习
2022-07-24 21:46:25
Android开发RecyclerView实现折线图效果
2022-08-27 12:59:34
![](https://img.aspxhome.com/file/2023/5/108325_0s.jpg)
java实现微信扫码支付功能
2023-11-09 18:38:08
解决grails服务端口冲突的办法(grails修改端口号)
2023-09-12 01:00:03
C#中list用法实例
2022-07-02 13:57:00
asp.net页面中如何获取Excel表的内容
2022-11-18 11:40:31
Android数据持久化之File机制分析
2021-05-29 19:24:56
解决IDEA的Terminal中文乱码问题
2022-05-06 01:31:08
![](https://img.aspxhome.com/file/2023/8/111438_0s.png)
SpringBoot打jar包遇到的xml文件丢失的解决方案
2023-04-11 23:39:06
![](https://img.aspxhome.com/file/2023/4/66174_0s.png)
Java 读取外部资源的方法详解及实例代码
2023-01-27 21:19:18
SpringMVC文件上传 多文件上传实例
2023-05-01 02:25:31
Flutter图片与文件选择器使用实例
2023-07-17 09:11:12
![](https://img.aspxhome.com/file/2023/0/106030_0s.png)
Java中this,static,final,const用法详解
2022-01-23 17:21:41
C#实现的Win32控制台线程计时器功能示例
2022-12-19 20:29:20
详解Android中Handler的使用方法
2023-05-03 06:35:30
![](https://img.aspxhome.com/file/2023/8/101858_0s.png)