【问题标题】:how to set materialButton icon to the center如何将materialButton图标设置为中心
【发布时间】:2019-01-09 00:21:50
【问题描述】:

我正在使用 supportLibrary = "28.0.0-beta01" 版本。
这是我在 .xml 文件中的代码:

<android.support.design.button.MaterialButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:icon="@drawable/ic_my_orders"
    app:iconGravity="textStart"
    android:gravity="center"/>

到按钮左侧的可绘制对象的代码图标。我想将按钮设置为中心。 我想达到这个结果


编辑

我不需要任何自定义视图或硬编码的东西。如果这是一个错误(app:iconGravity),我会等待下一个版本。

编辑

28.0.0-rc01版本修复的bug,换个版本就好了。

【问题讨论】:

  • 所以目前正在发生的事情>
  • 将您的MaterialButton 包裹在另一个LinearLayout 中,并使您的MaterialButton 宽度为wrap_content
  • @MuhammadakbarRafiqov 您使用的是哪个库,我使用了实现 'com.android.support:design:26.1.0' 但我没有选择 Materialbutton
  • @MuhammadakbarRafiqov 使用 LinearLayoutandroid:layout_width="match_parent" 并使用 width to wrap_content 在该布局中添加您的 MaterialButton
  • @RajshreeTiwari 'com.android.support:design:28.0.0-beta01' mdc

标签: android android-button material-components


【解决方案1】:

使用 Material Components 库只需使用 app:iconGravity="textStart" 属性:

 <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.Icon"
        app:icon="@drawable/..."
        app:iconGravity="textStart"
        ../>

如果你想偷工减料,请使用 app:shapeAppearanceOverlay 属性:

 <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.Icon"
        app:icon="@drawable/..."
        app:iconGravity="textStart"
        app:shapeAppearanceOverlay="@style/Button.Cut"
        ../>

与:

  <style name="Button.Cut" parent="">
    <item name="cornerFamily">cut</item>
    <item name="cornerSize">4dp</item>
  </style>

【讨论】:

    【解决方案2】:

    您在原始问题中的代码 sn-p 是正确的。这已被确定为一个错误,将在即将发布的版本中修复。

    【讨论】:

    • 在 '28.0.0-rc01' 版本中修复的错误
    【解决方案3】:

    更新

    尝试使用此代码设置按钮的左填充以使其居中。

     <android.support.design.button.MaterialButton
        android:id="@+id/material_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:gravity="center_vertical|start"
        android:text="CENTER"
        android:textColor="@android:color/white"
        app:icon="@drawable/ic_location_on_accent_24dp"
        app:layout_constraintBottom_toBottomOf="parent" />
    

    JAVA

     final MaterialButton button = root_view.findViewById(R.id.material_button);
     button.post(new Runnable() {
            @Override
            public void run() {
    
                int width = button.getWidth();
                button.measure(0,0);
                int paddingleft = (width - button.getMeasuredWidth() + 50)/2; //50 drawable width
                button.setPadding(paddingleft,0,0,0);
            }
        });
    

    输出

    【讨论】:

    • 仅供参考 app:additionalPaddingLeftForIcon="100dp" app:additionalPaddingRightForIcon="80dp"com.android.support:appcompat-v7:28.0.0-beta01 中不可用
    • @NileshRathod 我没有注意到我正在使用implementation 'com.android.support:appcompat-v7:28.0.0-alpha1' 感谢您提供信息。
    • @MuhammadakbarRafiqov 看到我更新的答案会帮助你。
    【解决方案4】:

    您可以使用宽度来包裹内容,使其与您的文本相匹配。并且 layout_gravity 而不是重力来设置按钮自己的重力(而不是它的孩子)。

    <android.support.design.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:icon="@drawable/ic_my_orders"
        app:iconGravity="textStart"
        android:layout_gravity="center"/>
    

    【讨论】:

      猜你喜欢
      • 2019-03-26
      • 2021-03-29
      • 2016-03-03
      • 1970-01-01
      • 2019-07-16
      • 2013-03-17
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      相关资源
      最近更新 更多