gpt4 book ai didi

android - 如何将宽度设置为 "fill parent"的 android 按钮中的图标和文本居中

转载 作者:IT老高 更新时间:2023-10-28 13:02:36 31 4
gpt4 key购买 nike

我想要一个带有图标+文本的 Android Button。我正在使用 drawableLeft 属性来设置图像,如果按钮的宽度为 "wrap_content" 但我需要拉伸(stretch)到最大宽度,所以我使用宽度 "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" />

enter image description here

为了使用 Material 组件,您显然需要:

添加依赖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>

关于android - 如何将宽度设置为 "fill parent"的 android 按钮中的图标和文本居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3634191/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com