小编Amr*_*Saj的帖子

将 MaterialToggleButton 的选定颜色更改为纯色

无法将 MaterialToggleButton 的选定颜色设置为纯色,仅显示原色的透明阴影。

我尝试了下面的代码集,输出图像如下所示。

样式中的按钮主题

 <style name="ThemeButtonColorToggle" parent="AppTheme">
        <item name="colorPrimary">@color/colorOrange</item>
        <item name="colorButtonNormal">@color/colorOrange</item>
        <item name="android:backgroundTint">@color/black</item>
        <item name="strokeColor">@color/colorOrange</item>
        <item name="strokeWidth">@dimen/mtrl_btn_stroke_size</item>
        <item name="colorOnPrimary">@color/colorOrange</item>
        <item name="colorOnPrimarySurface">@color/colorOrange</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

XML代码

    <com.google.android.material.button.MaterialButtonToggleGroup
        android:id="@+id/toggleGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:checkedButton="@id/btnAndroid"
        app:layout_constraintTop_toBottomOf="@id/tvName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:selectionRequired="true"
        app:singleSelection="true">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btnAndroid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textIsSelectable="true"
            android:theme="@style/ThemeButtonColorToggle"
            android:textColor="@android:color/white"
            android:textColorHighlight="@color/colorOrange"
            android:text="Android" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btniOS"
            android:layout_width="wrap_content"
            android:textIsSelectable="true"
            android:textColor="@android:color/white"
            android:theme="@style/ThemeButtonColorToggle"
            android:layout_height="wrap_content"
            android:text="iOS" />

    </com.google.android.material.button.MaterialButtonToggleGroup>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我需要得到如下所示的纯色

在此输入图像描述

谁能帮我解决一下

android android-button android-togglebutton material-components-android

12
推荐指数
1
解决办法
5336
查看次数

以编程方式将单选按钮文本添加到按钮顶部

有没有办法以编程方式将单选按钮的文本与顶部对齐,如下所示。

在此处输入图片说明

我使用以下代码创建无线电组

  final RadioButton[] rb = new RadioButton[5];
            RadioGroup rg = new RadioGroup(getActivity()); //create the RadioGroup
            rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL
            rg.setLayoutParams(radioparams);

            for (int i = 0; i < 5; i++) {
                rb[i] = new RadioButton(getActivity());
                rb[i].setText("Radiobtn " + i);
                rb[i].setId(i + 100);
                rg.addView(rb[i]);

            }
            layout.addView(rg);
Run Code Online (Sandbox Code Playgroud)

但我将文本放在每个按钮的右侧。

android radio-group radio-button android-layout

3
推荐指数
1
解决办法
1249
查看次数

动态地将布局包含到片段内的另一个布局文件中

我有一个片段布局,我想动态添加另一个子布局,

我试过这个/sf/answers/1581458861/

子布局xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:hint="Question"
    android:textColor="@color/colorTextBlack"
    android:textSize="16dp" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

这是我的主要布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f2f2"
android:orientation="vertical">
<FrameLayout
    android:id="@+id/flContainer"
    android:layout_width="match_parent"
    android:layout_margin="20dp"
    android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在我的片段中我包含了子布局

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_start_question, container, false);
     flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
     flContainer.addView(R.layout.sub_layout);
    return v;
}
Run Code Online (Sandbox Code Playgroud)

但我在addView附近收到错误截图如下 在此输入图像描述

任何人都可以帮助找到错误

android include fragment android-layout android-fragments

1
推荐指数
1
解决办法
1701
查看次数

想要打印"\n"符号而不需要换行

我的strings.xml中有一个字符串,它们之间有"\n"符号

<string name="example">Hello\nworld</string>
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能得到"\n"符号而不是新行?

android string.xml

0
推荐指数
1
解决办法
55
查看次数

如何在Android中显示这种类型的TabLayout选项卡?

在布局中显示此类型的选项卡

在此处输入图片说明

我尝试了但没有得到正确的输出

我的代码在这里

private int[] imageList= {R.drawable.icon_category,R.drawable.icon_newest};

 tabLayout=(TabLayout)findViewById(R.id.tabs);
    tabLayout.addTab(tabLayout.newTab().setText(""));
    tabLayout.addTab(tabLayout.newTab().setText(""));


    for (int i = 0; i < imageList.length; i++) {
        tabLayout.getTabAt(i).setIcon(imageList[i]);
    }
Run Code Online (Sandbox Code Playgroud)

android android-tabs android-tablayout

-1
推荐指数
1
解决办法
771
查看次数