如何为自定义数字选择器创建边框?

时间:2019-06-03 21:24:07

标签: android android-layout custom-controls mobile-development

我试图创建水平数字选择器,但是EditText的高度越来越大。

我在styles.xml中用<item name="android:background">@null</item>删除了EditText的底部边框

layout/test.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp">
    <LinearLayout
        android:id="@+id/num_picker_layout"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:orientation="horizontal"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/btn_product_to_basket"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/btn_less"
            style="@style/numberPickerBtn"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:text="-" />

        <EditText
            android:id="@+id/et_number"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:inputType="number"
            style="@style/numberPickerEt"
            android:text="1"
            android:textAlignment="center" />

        <Button
            android:id="@+id/btn_more"
            style="@style/numberPickerBtn"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:text="+" />
    </LinearLayout>

    <Button
        android:id="@+id/btn_product_to_basket"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Добавить в корзину"
        android:textAllCaps="false"
        android:textSize="14sp"
        app:icon="@drawable/ic_btn_basket"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toEndOf="@id/num_picker_layout"
        app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

drawable / number_picker.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <stroke android:width="1dp" android:color="#CCCCCC"/>
</shape>

styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="numberPickerBtn">
        <item name="backgroundTint">#E5E5E5</item>
        <item name="android:textColor">#757575</item>
        <item name="android:background">@drawable/number_picker</item>
    </style>
    <style name="numberPickerEt" parent="Widget.AppCompat.EditText">
        <item name="android:background">@drawable/number_picker</item>
    </style>
</resources>

我想要的结果: https://habrastorage.org/webt/5c/f4/b3/5cf4b3cc1a4e5823043866.jpeg

当前状态: https://habrastorage.org/webt/5c/f4/f1/5cf4f1d8911ce534938275.png

在这里我用红色填充以显示与父项匹配的按钮,但它们不匹配 https://habrastorage.org/webt/5c/f4/f7/5cf4f799b9116853603442.png

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案 connection()