【问题标题】:Android Setting material button top and bottom inset programmatically (Java)Android以编程方式设置材料按钮顶部和底部插入(Java)
【发布时间】:2019-04-14 02:54:55
【问题描述】:

我以编程方式创建了一个材质按钮,我希望它位于屏幕底部,这就像完全填充底部一样,但它的问题是有一个默认的底部插图使得底部空白区域显着。

要删除 XML 中的材料按钮的插图很容易,只需使用

android:insetLeft 机器人:insetRight 机器人:插图顶部 android:insetBottom

但我的问题是如何以编程方式设置插入值。

我发现下面的链接对于了解材质按钮非常有用 https://github.com/material-components/material-components-android/blob/master/docs/components/MaterialButton.md

【问题讨论】:

    标签: android button material-components-android


    【解决方案1】:

    material-components-1.3.0-alpha03中实现了对以编程方式设置顶部和底部插图的支持

    在 Kotlin 中你可以这样做:

    val materialButton = MaterialButton(ctx)
    materialButton.insetTop = 0
    materialButton.insetBottom = 0
    

    【讨论】:

      【解决方案2】:

      我也在努力寻找解决方案。找到了。您需要为没有插入的按钮创建一个样式,并在您的应用主题的materialButtonOutlinedStyle attr 中定义它。不要忘记在 MaterialButton ctor 中指定此类 attr。像这样:

      styles.xml:

      <style name="AppTheme" parent="Theme.MaterialComponents" >
          <item name="colorPrimary">@color/colorPrimary</item>
          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
          <item name="colorAccent">@color/colorAccent</item>
          < ... />
          <!-- The following defines the OutlinedButton style with your custom attrs -->
          <item name="materialButtonOutlinedStyle">@style/Widget.MaterialComponents.Button.OutlinedButton.NoInsets</item>
      </style>
      
      <style name="Widget.MaterialComponents.Button.OutlinedButton.NoInsets"
          parent="Widget.MaterialComponents.Button.OutlinedButton" >
          <!-- Here you can customize the drawable insets! -->
          <item name="android:insetTop">0dp</item>
          <item name="android:insetBottom">0dp</item>
      </style>
      

      活动/代码/Java 中的任何内容:

      private void addButtons() {
          MaterialButtonToggleGroup buttonGroup = findViewById(R.id.preference_selection_button_group);
          final ViewGroup.LayoutParams lp = new LinearLayout.LayoutParams(
                  ViewGroup.LayoutParams.WRAP_CONTENT,
                  ViewGroup.LayoutParams.WRAP_CONTENT
          );
      
          final int n = mEntries.length;
          for (int i = 0; i < n; i++)
              buttonGroup.addView(createChoiceButton(i, mEntries[i]), i, lp);
      }
      
      private View createChoiceButton(int index, CharSequence entry) {
          final MaterialButton button = new MaterialButton(
                  getContext(), 
                  null, 
                  R.attr.materialButtonOutlinedStyle /** which points to your new OutlinedButton theme **/);
          button.setId(index);
          button.setText(entry);
          return button;
      }
      

      布局:

      <parentLayout ...>
      
          <com.google.android.material.button.MaterialButtonToggleGroup
              android:id="@+id/preference_selection_button_group"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              app:singleSelection="true"
              app:selectionRequired="true" />
      
      </parentLayout>
      

      此代码用于 VERTICAL MaterialButtonToggleGroup,结果如下:

      【讨论】:

        【解决方案3】:

        据我所知,在 MaterialButton 源代码中没有设置 insets 的公共方法:

        https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/button/MaterialButtonHelper.java

        因此,您需要复制源代码并添加类似 setInsets() 方法的内容。 检查下面的此链接,它与您的问题类似: How to Change InsetDrawable Inset value after creation

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-04-10
          • 2016-05-09
          • 2020-06-04
          • 2023-03-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多