[英]drawableStart not showing drawable
我正在学习android,书中的挑战之一说要自己添加上一个按钮。 我做到了,并请一位朋友帮忙将箭头放在左侧,他做到了,但他不明白为什么drawableStart
不起作用。 ( drawableLeft
确实有效)
有人知道为什么drawableStart
不起作用吗? 或者更好的是:如何修复它?
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/previous_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/arrow_left"
android:text="@string/previous_button"
android:drawablePadding="4dp"/>
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/arrow_right"
android:drawablePadding="4dp"
android:text="@string/next_button"/>
</LinearLayout>
themes.xml下的代码:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.GeoQuiz" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
看来您正在使用材料设计按钮,所以使用android:icon
而不是android:drawableStart
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/previous_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/arrow_left"
android:text="@string/previous_button"
android:drawablePadding="4dp" />
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:icon="@drawable/arrow_right"
android:drawablePadding="4dp"
android:text="@string/next_button"/>
</LinearLayout>
更新:
没用。 现在它不显示任何一个箭头。 并且程序不再在模拟器中运行
似乎是与android
指令的兼容性问题,因此,将其更改为app
指令,并在使用时添加Widget.MaterialComponents.Button.TextButton.Dialog
样式
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/previous_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:icon="@drawable/arrow_left"
android:text="@string/previous_button"
style="@style/Widget.MaterialComponents.Button.TextButton.Dialog"
android:drawablePadding="4dp" />
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
app:icon="@drawable/arrow_right"
android:drawablePadding="4dp"
android:text="@string/next_button"/>
style="@style/Widget.MaterialComponents.Button.TextButton.Dialog"
</LinearLayout>
经过大量搜索,我终于想出了如何让它工作 android:drawableStart 不工作似乎是一个问题,这里是问题的链接并修复https://github.com/material-components/material-components -android/问题/1174
这是修复
<Button
android:id="@+id/previous_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:icon="@drawable/arrow_left"
app:iconGravity="textStart"
app:iconTint="@android:color/white"
android:text="@string/previous_button"
android:drawablePadding="4dp"/>
而不是使用 drawableBottom|Top|Left|... 使用 MaterialButton 的属性 app:icon, app:iconGravity 和 app:iconTint
注意 drawableStart 似乎是唯一一个与其他人一起工作的问题
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text="@string/your_text"
android:drawableEnd="@drawable/your_image"/>
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text="@string/your_text"
android:drawableTop="@drawable/your_image"/>
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text="@string/your_text"
android:drawableBottom="@drawable/your_image"/>
然后左
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text="@string/your_text"
app:icon="@drawable/arrow_left"
app:iconGravity="textStart"
app:iconTint="@android:color/white"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.