Я создаю изображение, подобное EditText. (На самом деле, я сделал это.)
Создал drawable xml
и установил EditText в качестве фона.
Однако, чтобы упростить управление этим, я создал стиль в theme
и присвоил его свойству style
в EditText
, но он не применяется.
В чем причина?
(Или, если есть лучший способ создать эти EditTexts, пожалуйста, дайте мне знать)
--
рисуемый
<shape xmlns:android = "http://schemas.android.com/apk/res/android"
android:shape = "rectangle">
<corners android:radius = "5dp"/>
<solid android:color = "@color/gray_400"/>
</shape>
тема.xml
<!-- memo EditText style -->
<style name = "MemoEditTextStyle" parent = "Widget.AppCompat.EditText">
<item name = "background">@drawable/edittext_memo</item>
<item name = "android:textColor">@color/purple_200</item>
<item name = "android:textColorHighlight">@color/gray_400</item>
<item name = "colorControlActivated">@color/gray_400</item>
</style>
в xml
<EditText
android:layout_width = "match_parent"
android:layout_height = "50dp"
style = "@style/MemoEditTextStyle"
android:hint = "@string/hint_memo"
android:paddingLeft = "10dp"
android:gravity = "center_vertical"/>
результат
Фон не применяется к EditText, потому что в стиле вы определяете свойство <item name = "background">
вместо <item name = "android:background">
.
Тот же результат может быть достигнут с помощью Widget.MaterialComponents
без необходимости рисования xml-формы, как в приведенном ниже примере:
Определите собственный стиль TextInputLayout.OutlinedBox:
<style name = "Custom.TextInputLayout.OutlinedBox" parent = "Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name = "boxBackgroundMode">outline</item>
<item name = "boxBackgroundColor">#c2c0ba</item>
<item name = "boxStrokeColor">#c2c0ba</item>
<item name = "boxStrokeErrorColor">#FF0000</item>
<item name = "boxCornerRadiusTopStart">5dp</item>
<item name = "boxCornerRadiusTopEnd">5dp</item>
<item name = "boxCornerRadiusBottomStart">5dp</item>
<item name = "boxCornerRadiusBottomEnd">5dp</item>
<item name = "boxStrokeWidth">0dp</item>
<item name = "boxCollapsedPaddingTop">0dp</item>
<item name = "hintEnabled">false</item>
<item name = "errorIconDrawable">@null</item>
<item name = "errorTextColor">#FF0000</item>
<item name = "android:textColorHint">#B3B3B3</item>
<item name = "materialThemeOverlay">@style/Custom.ThemeOverlay.TextInputEditText.OutlinedBox</item>
</style>
<style name = "Custom.ThemeOverlay.TextInputEditText.OutlinedBox" parent = "@style/ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
<item name = "editTextStyle">@style/Custom.TextInputEditText.OutlinedBox</item>
</style>
<style name = "Custom.TextInputEditText.OutlinedBox" parent = "@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox">
<item name = "android:paddingTop">16dp</item>
<item name = "android:paddingBottom">16dp</item>
<item name = "android:paddingStart">10dp</item>
<item name = "android:paddingEnd">10dp</item>
<item name = "android:paddingLeft">10dp</item>
<item name = "android:paddingRight">10dp</item>
<item name = "android:textColor">@color/white</item>
<item name = "android:textColorHint">@color/black</item>
</style>
Использование:
<com.google.android.material.textfield.TextInputLayout
style = "@style/Custom.TextInputLayout.OutlinedBox"
android:layout_width = "match_parent"
android:layout_height = "wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:hint = "MEMO"/>
</com.google.android.material.textfield.TextInputLayout>
Результат:
@ybybyb parent = "@style/..." используется, когда вы хотите наследовать стили из библиотеки или собственного проекта. Это необязательно, поэтому вы можете удалить его, если хотите. Основная цель этого — отличить от parent = "@android:style/". Что касается второго вопроса, то это проблема, связанная с IDE, но обычно она должна выполняться автоматически.
Спасибо. Я просто задам вам два вопроса.
1.
Когда@style
используется вparent = "@style/..."
?2.
ВIDE
,@style/Widget.MaterialComponents.
или@style/ThemeOverlay.MaterialComponents.
После написанияTextInputEditText.OutlinedBox
не выполняется автозаполнение, но ошибки не возникает. По какой причине?