Android ボタンにアイコンとテキストを中央に配置したいです。drawableLeft 属性を使用して画像を設定します。ボタンの幅が の場合はこれでうまく機能します"wrap_content"
が、最大幅まで伸ばす必要があるため、 width を使用します"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" />
それを実現する方法について何か提案はありますか?
ベストアンサー1
これまでの回答はすべて時代遅れのようです
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>