首页 > 解决方案 > Android - 重量选择器

问题描述

我来自 iOS 和 Swift,我真的不知道如何简单地替换PickerView.

在我的应用程序中,用户应该能够定义它的重量(例如 75.6 公斤)。

最好的方法似乎是这样的: 在此处输入图像描述

鉴于我是 Android 新手,我真的不知道如何重现它。我做了一些研究,它似乎是一个NumberPicker?但我不知道如何拥有这两个不同的列。

标签: javaandroiduipickerviewpickernumberpicker

解决方案


在此处输入图像描述

NumberPicker通过使用并排放置的两个 s,您可以通过一些自定义来实现类似的效果:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal">

    <NumberPicker
        android:id="@+id/first_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:theme="@style/CustomNumberPickerTheme" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="."
        android:textSize="24sp" />

    <NumberPicker
        android:id="@+id/second_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:theme="@style/CustomNumberPickerTheme" />
</LinearLayout>

为了更好地匹配屏幕截图,aTextView包含在两个NumberPickers 之间作为小数点以及CustomNumberPickerTheme您需要在其中设置的自定义主题res/values/styles.xml

<resources>

    ...

    <style name="CustomNumberPickerTheme" parent="AppTheme">
        <item name="colorControlNormal">@android:color/transparent</item>
    </style>
</resources>

推荐阅读