代码之家  ›  专栏  ›  技术社区  ›  KevinB

Android-重量选择器

  •  1
  • KevinB  · 技术社区  · 7 年前

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

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

    最好的办法是这样:

    鉴于我是安卓系统的新手,我真的不知道如何复制它。我做了一些研究,它似乎是一个 numberpicker ?但我不知道如何才能拥有这两个不同的列。

    最好的办法是这样: enter image description here

    鉴于我是安卓系统的新手,我真的不知道如何复制它。我做了一些调查 NumberPicker ?但我不知道如何才能有这两个不同的专栏。

    1 回复  |  直到 7 年前
        1
  •  3
  •   jak10h    7 年前

    您可以通过使用两个 numberpicker s并排放置来实现类似的定制:

    <linearlayout xmlns:android=“http://schemas.android.com/apk/res/android”
    android:layout_width=“匹配父级”
    android:layout_height=“匹配父级”
    android:gravity=“中心”
    android:orientation=“水平”>
    
    <数字选取器
    android:id=“@+id/第一个号码”
    android:layout_width=“包装内容”
    android:layout_height=“包装内容”
    android:theme=“@style/customNumberPickerTheme”/>
    
    文本视图
    android:layout_width=“包装内容”
    android:layout_height=“包装内容”
    安卓:text=“”
    android:textsize=“24sp”/>
    
    <数字选取器
    android:id=“@+id/秒\号码”
    android:layout_width=“包装内容”
    android:layout_height=“包装内容”
    android:theme=“@style/customNumberPickerTheme”/>
    </linearlayout>
    < /代码> 
    
    

    为了更好地匹配屏幕截图,Atextviewwas included in between the twonumberpickers as the decimal point with a custom themecustomnumberpickerTheme.which you'll need to set insideres/values/styles.xml.

    资源 … <style name=“CustomNumberPickerTheme”parent=“AppTheme”> <item name=“colorcontrolNormal”>@android:color/transparent</item> &风格/风格; </resources> < /代码>

    您可以通过使用两个NumberPickerS并排放置:

    <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包括在两人之间数字选择器s作为小数点和自定义主题CustomNumberPickerTheme你需要把它放在里面res/values/styles.xml

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