Почему не отображается текст кнопок в разметке Android xml?
Я делаю android приложение с кастомной клавиатурой. Создавая разметку клавиатуры я наткнулся на странную проблему, не отображаются буквы на кнопках Button при запуске на реальном устройстве. Если конкретнее, текст не отображается при запуске на смартфоне Honor(отображаются просто "голые" кнопки без текста, либо очень сильно обрезанные), но при запуске на Samsung все отображается.
Всю разметку скинуть не смогу(слишком большая), но постараюсь выделить главное.
Разметка кнопки:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnW"
android:layout_weight="1"
android:text="w"
style="@style/keyBoardBtnStyle"/>
Стиль кнопки:
<style name="keyBoardBtnStyle" parent="Widget.AppCompat.Button">
<item name="android:background">@drawable/btn_shape</item>
<item name="android:textColor">@color/white</item>
<item name="android:layout_margin">2dp</item>
<item name="android:maxLines">1</item>
<item name="android:textAllCaps">false</item>
</style>
Drawable кнопки(использую ripple-эффект):
<ripple android:color="@color/white" xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="@color/sky"/>
<corners android:radius="12dp"/>
</shape>
</item>
</ripple>
Кнопки располагаются линиями(LinearLayout с горизонтальной ориентацией). Вот пример линии с кнопками:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginHorizontal="5dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="10">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnQ"
android:layout_weight="1"
android:text="q"
style="@style/keyBoardBtnStyle"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnW"
android:layout_weight="1"
android:text="w"
style="@style/keyBoardBtnStyle"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnE"
android:layout_weight="1"
android:text="e"
style="@style/keyBoardBtnStyle"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnR"
android:layout_weight="1"
android:text="r"
style="@style/keyBoardBtnStyle"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnT"
android:layout_weight="1"
android:text="t"
style="@style/keyBoardBtnStyle"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnY"
android:layout_weight="1"
android:text="y"
style="@style/keyBoardBtnStyle"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnU"
android:layout_weight="1"
android:text="u"
style="@style/keyBoardBtnStyle"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnI"
android:layout_weight="1"
android:text="i"
style="@style/keyBoardBtnStyle"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnO"
android:layout_weight="1"
android:text="o"
style="@style/keyBoardBtnStyle"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnP"
android:layout_weight="1"
android:text="p"
style="@style/keyBoardBtnStyle"/>
</LinearLayout>
Как выглядит themes:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.App" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Customize your light theme here. -->
<!-- <item name="colorPrimary">@color/my_light_primary</item> -->
</style>
<style name="Theme.App" parent="Base.Theme.App" />
</resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.App" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Customize your dark theme here. -->
</style>
</resources>
Я не понимаю из-за чего так происходит. Перепробовал уже все, ставил и изменял атрибуты padding, maxLines, textAllCaps, textSize не помогло. Шрифт стандартный. Буду рад любой помощи.
Ответы (1 шт):
Устройства Honor (особенно на EMUI) иногда странно рендерят Button, особенно в кастомных клавиатурах. У меня была такая же проблема — текст на кнопках просто не показывался или обрезался.
Вот что помогло:
1. layout_width="0dp" вместо wrap_content при layout_weight:
Классика. Когда у кнопки wrap_content и одновременно layout_weight, LinearLayout просто не понимает, сколько ей дать места. Особенно на EMUI.
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="w"
style="@style/keyBoardBtnStyle"/>
2. Добавить minHeight и minWidth:
Без этого текст может "упереться" в границы и не отрендериться.
<item name="android:minHeight">48dp</item>
<item name="android:minWidth">48dp</item>
3. Явно указать textSize:
На Honor даже TextView бывает не отображается, если размер не указан.
<item name="android:textSize">16sp</item>
4. Проверь настройки телефона (масштаб):
Honor может увеличивать шрифт/элементы на системном уровне. Это влияет даже на стандартные компоненты.
Финальный стиль кнопки:
<style name="keyBoardBtnStyle" parent="Widget.AppCompat.Button">
<item name="android:background">@drawable/btn_shape</item>
<item name="android:textColor">@color/white</item>
<item name="android:layout_margin">2dp</item>
<item name="android:maxLines">1</item>
<item name="android:textAllCaps">false</item>
<item name="android:textSize">16sp</item>
<item name="android:minHeight">48dp</item>
<item name="android:minWidth">48dp</item>
</style>
После этих правок всё заработало и на Samsung, и на Honor.