在Android中以编程方式创建具有自定义样式的MaterialButtonToggleGroup,包括MaterialButton(s)。

3
我正在尝试创建一个包含Material按钮的MaterialButtonToggleGroup,并希望使用自定义样式来完成。我已经能够使用xml创建,但是我需要通过编程实现相同的结果。
以下是我在xml中所做的(我使用LinearLayout作为根视图):
<com.google.android.material.button.MaterialButtonToggleGroup
  android:id="@+id/group_item_size"
  style="@style/ButtonsToggleGroupStyle"
  >

  <com.google.android.material.button.MaterialButton
    style="@style/ButtonStyle"
    android:text="Small"
    />

  <com.google.android.material.button.MaterialButton
    style="@style/ButtonStyle"
    android:text="Large"
    />

  <com.google.android.material.button.MaterialButton
    style="@style/ButtonStyle"
    android:text="Family"
    />

</com.google.android.material.button.MaterialButtonToggleGroup>

以及样式的代码:

      <style name="ButtonStyle" parent="Widget.MaterialComponents.Button.UnelevatedButton">
    <item name="cornerRadius">@dimen/margin_20dp</item>
    <item name="android:padding">@dimen/margin_8dp</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_weight">1</item>
    <item name="android:textSize">@dimen/text_size_14sp</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:textColor">@drawable/drawable_button_toggle_group_text_selector</item>
    <item name="android:backgroundTint">@drawable/drawable_button_toggle_group_selector</item>
  </style>

  <style name="ButtonsToggleGroupStyle">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_gravity">center</item>
    <item name="android:orientation">horizontal</item>
    <item name="android:layout_marginStart">@dimen/margin_32dp</item>
    <item name="android:layout_marginTop">@dimen/margin_12dp</item>
    <item name="android:layout_marginEnd">@dimen/margin_32dp</item>
  </style>

thanks in advance for your help

3个回答

7

只需使用一个xml布局(single_button_layout.xml)来定义单个MaterialButton,并使用您喜欢的样式:

<com.google.android.material.button.MaterialButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/ButtonStyle"
    .../>

那么就可以使用以下类似的方法:

MaterialButtonToggleGroup group = findViewById(R.id.toggle_button_group);

for (....){

  MaterialButton button =
      (MaterialButton)  getLayoutInflater().inflate(R.layout.single_button_layout, group, false);
  button.setText(...);
  group.addView(button);

}

1

试试这个,只需将它放在 .java 文件中,而无需在 xml 文件中添加任何内容。

  layout=findViewById(R.id.layout);
      
        MaterialButtonToggleGroup grupo = new MaterialButtonToggleGroup(this, null, com.google.android.material.R.attr.materialButtonOutlinedStyle);
        MaterialButton btn1= new MaterialButton(this, null, com.google.android.material.R.attr.materialButtonOutlinedStyle);
        btn1.setText("Ejemplo 1");

        MaterialButton btn2= new MaterialButton(this, null, com.google.android.material.R.attr.materialButtonOutlinedStyle);
        btn2.setText("Ejemplo 2");
        MaterialButton btn3=new MaterialButton(this, null, com.google.android.material.R.attr.materialButtonOutlinedStyle);
        btn3.setText("Ejemplo 3");
        grupo.addView(btn1);
        grupo.addView(btn2);
        grupo.addView(btn3);
        layout.addView(grupo);

1
你的回答可以通过添加更多支持信息来改进。请[编辑]以添加进一步的细节,例如引用或文档,以便他人可以确认你的回答是否正确。您可以在帮助中心找到有关编写良好答案的更多信息。 - Community

0

如果要全部以编程方式完成,您可以按照以下方式编写框架:

MaterialButtonToggleGroup charpathButtongroup = new MaterialButtonToggleGroup(this, null, R.attr.materialButtonOutlinedStyle);
MaterialButton pathButton[] = new MaterialButton[2];
pathButton[0] = new MaterialButton(this, null, R.attr.materialButtonOutlinedStyle);
pathButton[1] = new MaterialButton(this, null, R.attr.materialButtonOutlinedStyle);
FrameLayout.LayoutParams charpathgroupParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
charpathButtongroup.setLayoutParams(charpathgroupParams);
game.addView(charpathButtongroup);
charpathButtongroup.addView(pathButton[0]);
charpathButtongroup.addView(pathButton[1]);

在将视图添加到 MaterialButtonToggleGroup 之前,请确保为其设置适当的参数,否则您将会收到以下异常:
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

可以通过以下方式在代码中编程添加属性到您的LayoutParams和MaterialButtons:

enter image description here

我更喜欢在我的MMORG游戏中以编程方式完成所有操作,而不需要布局文件夹。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接