一、自定义一个竖直Layout

垂直Layout

我们想到的第一直觉肯定是ViewGroup,自定义一个ViewGroup通常包含onMeasure(测量),onLayout(布局)。

onMeasure

onMeasure是一个父往子依次measure的过程。当我们要画一个竖直的Layout.我们应该得到下图:
在这里插入图片描述
父控件的高度是子控件的高度和。设自定义viewgroup有三个EditText

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.jhzl.customview.VerticalLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:layout_gravity="center">

        <EditText
            android:background="@android:color/holo_red_dark"
            android:text="hahah"
            android:layout_width="200dp"
            android:layout_height="100dp"/>


        <EditText
            android:background="@android:color/holo_green_dark"
            android:text="hahah"
            android:layout_width="200dp"
            android:layout_height="100dp"/>

        <EditText
            android:background="@android:color/holo_blue_dark"
            android:text="hahah"
            android:layout_width="200dp"
            android:layout_height="100dp"/>
    </com.jhzl.customview.VerticalLayout>

</FrameLayout>

此时我们应该复写VerticalLayout的onMeasure了

 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int totalHeight = 0;
        int totalWidth = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            //计算总共高度
            totalHeight = child.getMeasuredHeight() + totalHeight;
            //取最大的子view的宽度
            totalWidth = Math.max(child.getMeasuredWidth(), totalWidth);
        }
        float xmlMaxWidth = getResources().getDimension(R.dimen.dp250);
        float xmlMaxHeight = getResources().getDimension(R.dimen.dp100);
        Log.d(TAG,"xmlMaxWidth = "+xmlMaxWidth+"   xmlMaxHeight = "+xmlMaxHeight+"     totalWidth = "+(totalWidth)+"  totalHeight = "+totalHeight);
        setMeasuredDimension(totalWidth,totalHeight);
    }

实验打印结果:

xmlMaxWidth = 656.25   xmlMaxHeight = 262.5     totalWidth = 656  totalHeight = 789
 xmlMaxWidth = 656.25   xmlMaxHeight = 262.5     totalWidth = 656  totalHeight = 789

实验UI效果:
在这里插入图片描述
是黑的,这是因为我们还没有onLayout。好了,接下来我们来看一下怎么layout.

onLayout

测量完了,我们就可以根据测量的高度,来计算布局相应的位置啦。按照最初的设想,我们是要做垂直的Layout.那也就是子布局高度的想加:

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int curTop = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.layout(0,curTop,child.getMeasuredWidth(),curTop+child.getMeasuredHeight());
            curTop = curTop + child.getMeasuredHeight();
        }
    }

另外需要补充的知识点是,这里用的是MeasureHeight和MeasureWidth,为什么不用getHeight和getWidth呢,因为Measure开头的都是在onMeasure之后赋值的,而getHeight和getWidth则是在onLayout之后才有值。

接下来可以看实验结果了:
在这里插入图片描述
View被正常的layout到了界面上。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的竖直SeekBar的Java代码实现: 首先,在XML布局文件中添加一个SeekBar: ```xml <SeekBar android:id="@+id/vertical_seekbar" android:layout_width="wrap_content" android:layout_height="200dp" android:rotation="270" android:max="100" android:progress="0"/> ``` 在代码中,创建一个自定义的VerticalSeekBar类,继承自SeekBar,并且重写onMeasure()方法,使得SeekBar可以竖直方向显示。 ```java public class VerticalSeekBar extends SeekBar { public VerticalSeekBar(Context context) { super(context); } public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public VerticalSeekBar(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(heightMeasureSpec, widthMeasureSpec); setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); } @Override protected synchronized void onDraw(Canvas canvas) { canvas.rotate(-90); canvas.translate(-getHeight(), 0); super.onDraw(canvas); } } ``` 在Activity中,可以这样使用: ```java VerticalSeekBar verticalSeekBar = findViewById(R.id.vertical_seekbar); verticalSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // 这里可以处理SeekBar的进度变化事件 } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); ``` 这样就可以创建一个竖直方向的SeekBar了。注意,在XML布局文件中需要添加android:rotation="270"属性,将SeekBar旋转270度,才能让它竖直显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值