我想要一个 Android 按钮,其中图标+文本居中。我使用 drawableLeft 属性来设置图像,如果按钮的宽度为"wrap_content"
但我需要拉伸到最大宽度,所以我使用宽度"fill_parent"
。这会将我的图标直接移动到按钮的左侧,并且我希望图标和文本都在按钮内居中。
我尝试设置填充,但这只允许给出固定值,所以这不是我需要的。我需要将图标+文本居中对齐。
<Button
android:id="@+id/startTelemoteButton"
android:text="@string/start_telemote"
android:drawableLeft="@drawable/start"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:width="fill_parent"
android:heigh="wrap_content" />
关于我如何实现这一目标有什么建议吗?
以前的所有答案似乎都已过时
您可以使用MaterialButton
现在让我们设置图标重力。
<com.google.android.material.button.MaterialButton
android:id="@+id/btnDownloadPdf"
android:layout_width="0dp"
android:layout_height="56dp"
android:layout_margin="16dp"
android:gravity="center"
android:textAllCaps="true"
app:backgroundTint="#fc0"
app:icon="@drawable/ic_pdf"
app:iconGravity="textStart"
app:iconPadding="10dp"
app:iconTint="#f00"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="Download Pdf" />
要使用材料组件,您显然需要:
添加依赖项implementation 'com.google.android.material:material:1.3.0-alpha01'
(使用最新版本)
让您的主题扩展 Material Components 主题
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
...
</style>
如果您不能这样做,请从 Material Bridge 主题扩展它
<style name="AppTheme" parent="Theme.MaterialComponents.Light.Bridge">
...
</style>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)