一手遮天 Android - view(选择类): NumberPicker 样式

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - view(选择类): NumberPicker 样式

示例如下:

/view/selection/NumberPickerDemo2.java

/**
 * NumberPicker - 滑动选择框
 *
 * 本例演示如何自定义 NumberPicker 的样式(需要继承 NumberPicker 以便实现自定义逻辑,详见:NumberPickerCustom.java)
 */

package com.webabcd.androiddemo.view.selection;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import com.webabcd.androiddemo.R;
import com.webabcd.androiddemo.utils.Helper;

public class NumberPickerDemo2 extends AppCompatActivity {

    // 自定义的 NumberPicker
    private NumberPickerCustom _numberPicker1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_selection_numberpickerdemo2);

        _numberPicker1 = (NumberPickerCustom)findViewById(R.id.numberPicker1);

        sample();
    }

    private void sample() {
        _numberPicker1.setMinValue(0);
        _numberPicker1.setMaxValue(24);
        _numberPicker1.setValue(12);

        // 设置 NumberPicker 的分隔线的颜色
        _numberPicker1.setDividerColor(getResources().getColor(R.color.blue));
        // 设置 NumberPicker 的分隔线的粗细
        _numberPicker1.setDividerHeight(Helper.dp2px(this, 5));
    }
}

/layout/activity_view_selection_numberpickerdemo2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--
        本例演示如何自定义 NumberPicker 的样式
        NumberPickerCustom - 自定义的 NumberPicker(详见:view/selection/NumberPickerCustom.java)
    -->

    <com.webabcd.androiddemo.view.selection.NumberPickerCustom
        android:id="@+id/numberPicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-05-31 13:08  webabcd  阅读(520)  评论(0编辑  收藏  举报