android 仿IOS实现SegmentControl

android原本没有segment控件,如果要最简单的实现方法就用多个Button来更改背景颜色实现。

这里讲的是自定义一个控件。网上各路大牛也发过类似的。我这里也做一下记录。

美工切了个图,要实现一个这样的效果,代码大多都摘抄自网上,做了一点修改。具体原理就不多说了。

一、首先自定义资源文件xml代码如下:

<declare-styleable name="SegmentControl">
        <attr name="cornerRadius" format="dimension|reference" />
        <attr name="scv_BackgroundSelectedColor" format="reference|color" />//选中segment的背景颜色
        <attr name="scv_BackgroundNormalColor" format="reference|color" />//未选中segment的背景颜色
        <attr name="scv_TextSelectedColor" format="reference|color" />//选中segment的文字颜色
        <attr name="scv_TextNormalColor" format="reference|color" />//未选中segment的文字颜色
        <attr name="scv_FrameColor" format="reference|color" />//segment边框的颜色
        <attr name="scv_FrameWidth" format="reference|dimension" />//segment边框的宽度
        <attr name="scv_FrameCornerRadius" format="reference|dimension" />//segment四个圆角的半径大小
        <attr name="scv_TextSize" format="reference|dimension" />//文字大小
        <attr name="scv_TextArray" format="reference" />//string数组,每一个string都会填充到一个segment中
        <attr name="scv_SelectedIndex" format="reference|integer" />//默认选中的segment
        <attr name="scv_SegmentPaddingHorizontal" format="reference|dimension" />//每一个segment内部的水平padding
        <attr name="scv_SegmentPaddingVertical" format="reference|dimension" />每一个Segment的竖直方向的padding
        <attr name="scv_Gradient" format="reference|boolean" />//Segment改变时是否使用颜色渐变效果
    </declare-styleable>

二、自定义segment控件,这里用自定义view。代码如下:

package com.tang.segment.segmentcontrol;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.lang.reflect.Field;

/**
 * Created by tang on 2018/2/25.
 */

public class SegmentControl extends View {

    /**
     * onSegmentChanged function will be triggered if segment changed
     */
    public interface OnSegmentChangedListener{
        void onSegmentChanged(int newSelectedIndex);
    }

    private static final float TOUCHED_BACKGROUND_DARK_COEFFICIENT = 0.95F;

    private static final int COLOR_PRIMARY_NORMAL = 0XFFFFFFFF;
    private static final int COLOR_PRIMARY_SELECTED = 0XFF2CA99F;

    private static final int DEFAULT_COLOR_BACKGROUND_SELECTED = COLOR_PRIMARY_SELECTED;
    private static final int DEFAULT_COLOR_BACKGROUND_NORMAL = COLOR_PRIMARY_NORMAL;
    private static final int DEFAULT_COLOR_TEXT_SELECTED = COLOR_PRIMARY_NORMAL;
    private static final int DEFAULT_COLOR_TEXT_NORMAL = COLOR_PRIMARY_SELECTED;
    private static final int DEFAULT_COLOR_FRAME = COLOR_PRIMARY_SELECTED;
    private static final int DEFAULT_TEXT_SIZE_SP = 16;
    private static final int DEFAULT_FRAME_WIDTH_PX = 2;
    private static final int DEFAULT_FRAME_CORNER_RADIUS_PX = 0;
    private static final int DEFAULT_SELECTED_INDEX = 0;
    private static final int DEFAULT_SEGMENT_PADDING_HORIZONTAL = 16;
    private static final int DEFAULT_SEGMENT_PADDING_VERTICAL = 12;
    private static final boolean DEFAULT_IS_GRADIENT = false;

    private String[] mTexts = null;

    private int mColorBackgroundSelected = DEFAULT_COLOR_BACKGROUND_SELECTED;
    private int mColorBackgroundNormal = DEFAULT_COLOR_BACKGROUND_NORMAL;
    private int mColorTextSelected = DEFAULT_COLOR_TEXT_SELECTED;
    private int mColorTextNormal = DEFAULT_COLOR_TEXT_NORMAL;
    private int mColorFrame = DEFAULT_COLOR_FRAME;
    private int mFrameWidth = DEFAULT_FRAME_WIDTH_PX;
    private int mFrameCornerRadius = DEFAULT_FRAME_CORNER_RADIUS_PX;

    private int mTextSize = 0;
    private int mSelectedIndex = DEFAULT_SELECTED_INDEX;

    //used in wrap_content mode
    private int mSegmentPaddingHorizontal = DEFAULT_SEGMENT_PADDING_HORIZONTAL;
    private int mSegmentPaddingVertical = DEFAULT_SEGMENT_PADDING_VERTICAL;

    private boolean mIsGradient = DEFAULT_IS_GRADIENT;
    private OnSegmentChangedListener mOnSegmentChangedListener;

    private float unitWidth = 0;
    private Paint paintText;        //painter of the text
    private Paint paintBackground;  //painter of the background
    private Paint paintFrame;       //painter of the frame
    private RectF rectF;
    private RectF rectFArc;
    private Path pathFrame;

    private float textCenterYOffset;

    private int preTouchedIndex = -1;
    private int curTouchedIndex = -1;

    private ViewPager viewPager;

    public SegmentControl(Context context) {
        super(context);
        init();
    }
    public SegmentControl(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttr(context, attrs);
        init();
    }
    public SegmentControl(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttr(context, attrs);
        init();
    }

    private void initAttr(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SegmentControl);

        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            if(attr == R.styleable.SegmentControl_scv_BackgroundSelectedColor){
                mColorBackgroundSelected = a.getColor(attr, DEFAULT_COLOR_BACKGROUND_SELECTED);
            }else if(attr == R.styleable.SegmentControl_scv_BackgroundNormalColor){
                mColorBackgroundNormal = a.getColor(attr, DEFAULT_COLOR_BACKGROUND_NORMAL);
            }else if(attr == R.styleable.SegmentControl_scv_TextSelectedColor){
                mColorTextSelected = a.getColor(attr, DEFAULT_COLOR_TEXT_SELECTED);
            }else if(attr == R.styleable.SegmentControl_scv_TextNormalC
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍一下Android仿IOS自定义AlertDialog提示框的实现方法。 首先,我们需要在Android项目中创建一个自定义布局文件,用于显示弹框的内容。可以使用LinearLayout或RelativeLayout等布局容器来组织弹框的内容,例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="标题" android:textSize="18sp" /> <TextView android:id="@+id/message" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="内容" android:textSize="14sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/confirm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" /> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout> </LinearLayout> ``` 接下来,我们需要创建一个自定义AlertDialog类,用于显示弹框和处理按钮点击事件。在这个类中,我们需要实现onCreateDialog方法来加载自定义布局文件,并设置弹框的标题、内容和按钮监听器等。例如: ```java public class IOSAlertDialog extends DialogFragment { private String title; private String message; private DialogInterface.OnClickListener confirmListener; private DialogInterface.OnClickListener cancelListener; public IOSAlertDialog(String title, String message, DialogInterface.OnClickListener confirmListener, DialogInterface.OnClickListener cancelListener) { this.title = title; this.message = message; this.confirmListener = confirmListener; this.cancelListener = cancelListener; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { LayoutInflater inflater = LayoutInflater.from(getActivity()); View view = inflater.inflate(R.layout.dialog_ios_alert, null); TextView titleView = view.findViewById(R.id.title); TextView messageView = view.findViewById(R.id.message); Button confirmButton = view.findViewById(R.id.confirm); Button cancelButton = view.findViewById(R.id.cancel); titleView.setText(title); messageView.setText(message); confirmButton.setOnClickListener(confirmListener); cancelButton.setOnClickListener(cancelListener); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setView(view); return builder.create(); } } ``` 最后,在我们的Activity中,我们可以通过创建一个实例对象,并调用show方法来显示弹框。例如: ```java IOSAlertDialog dialog = new IOSAlertDialog( "提示", "确定要删除吗?", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 确定按钮点击事件 } }, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 取消按钮点击事件 } }); dialog.show(getSupportFragmentManager(), "IOSAlertDialog"); ``` 这样,我们就可以实现一个Android仿IOS自定义AlertDialog提示框了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值