标签: material-components-android

如何使 Android 中的材质切换按钮表现得像 Switch?

我正在使用以下材料设计库

implementation "com.google.android.material:material:1.2.0-alpha01"

这就是我实现材质切换按钮的方式

<com.google.android.material.button.MaterialButtonToggleGroup
    id="@+id/toggleBtnGroup"
    app:singleSelection="true"
    android:id="@+id/toggle_button_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:checkedButton="@+id/leftAlign">

    <com.google.android.material.button.MaterialButton
        id="@+id/leftAlign"
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Left"/>
    <com.google.android.material.button.MaterialButton
        id="@+id/centerAlign"
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Center"/>
    <com.google.android.material.button.MaterialButton
        id="@+id/rightAlign"
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Right"/>

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

现在,当我选择其他按钮时,一次仅选择 1 个按钮,如何确保如果用户再次选择所选按钮,它不会被取消选择。因此本质上,我希望在特定时间至少选择这些按钮之一。材质按钮切换

一个人会怎样做呢?

android android-button kotlin material-components material-components-android

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

如何更改 MaterialAlertDialog 中项目的文本颜色?

我正在尝试更改单个选择 MaterialViewAlertDialog 中项目的文本颜色。我实现了更改“RadioButton”颜色和标题颜色,但无法更改项目的文本颜色。现在我使用以下样式:

<style name="AlertDialog" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
        <item name="android:titleTextColor">@color/colorText</item>
        <item name="android:textColorSecondary">@color/colorText</item>
        <item name="android:textColorAlertDialogListItem">@color/colorText</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我还尝试使用 textAppearanceListItem 更改项目的文本颜色,但它不起作用。

android android-alertdialog material-design material-components material-components-android

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

如何使用材料设计主题使按钮变圆?

我希望我的按钮有圆角,例如:

圆形按钮// 图片取自google

要在 Android 中使用材质主题实现此目的,请将:设置shapeAppearanceSmallComponent为有一个rounded角。

但设置shapeAppearanceSmallComponent也会影响所有其他组件,例如EditText现在它们也被舍入。

因此,我没有将其设置为shapeAppearanceSmallComponent,而是创建了一个shapeMaterialOverlay. 将此覆盖设置为 abuttonStyle并将主题中的此按钮样式设置为默认按钮样式。

它有效,但仅适用于默认按钮。如果我需要TextButton这样的:

<Button
    ...
    style="@style/Widget.MaterialComponents.Button.TextButton"/>
Run Code Online (Sandbox Code Playgroud)

不会TextButton被舍入。因此,作为一种解决方法,我创建了MyTextButton从那里延伸TextButton并设置shapeOverlay在那里的样式。

所以现在如果我需要一个TextButton,我会这样做:

<Button
    ...
    style="@style/Widget.MaterialComponents.Button.MyTextButton"/>
Run Code Online (Sandbox Code Playgroud)

反而。

我必须对所有其他按钮类型执行此操作。我想知道这种方法是否正确,如果不正确,有人可以指导我如何正确执行此操作吗?

非常感谢。

android android-button material-design material-components material-components-android

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

如何在材质组件的dateRangePicker上使用setSelection?

我想在打开 dateRangePicker 时显示选定的日期范围,我知道 setSelected 方法就是用于此目的的,我尝试过它不起作用,或者可能做错了什么,请帮助

这是我已经尝试过的

public void openDateRangePicker(View view) {
 MaterialDatePicker.Builder builder =
                MaterialDatePicker.Builder.dateRangePicker();

        CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();

        MaterialDatePicker<Pair<Long,Long>> picker = builder.build();
        builder.setCalendarConstraints(constraintsBuilder.build());

        picker.setStyle(DialogFragment.STYLE_NORMAL, R.style.Custom_MaterialCalendar_Fullscreen);
        if(!firstDateStr.isEmpty() || !endDateStr.isEmpty()){
            builder.setSelection(selectionDates);

        }
        picker.show(getSupportFragmentManager(), picker.toString());



    picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
        @Override
        public void onPositiveButtonClick(Pair<Long, Long> selection) {

            long firstDateLong = selection.first;
            Date firstDate=new Date(firstDateLong);
            long endDateLong = selection.second;
            Date endDate=new Date(endDateLong);

            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

            //format yyyy-MM-dd
            firstDateStr = sdf2.format(firstDate);
            endDateStr = sdf2.format(endDate);
            selectionDates = selection;


            selectedDatesStr = firstDateStr + …
Run Code Online (Sandbox Code Playgroud)

java android daterangepicker material-components-android

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

如何使MaterialAlertDialogBu​​ilder居中对齐标题、消息和按钮

如何使标题、消息和按钮居中对齐

MaterialAlertDialogBuilder(requireContext())
                .setTitle("Message")
                .setMessage("This is info message.")
                .setPositiveButton("Ok") { dialog, which ->
             
                }
                
                .show()
Run Code Online (Sandbox Code Playgroud)

java android android-alertdialog kotlin material-components-android

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

如何在Android中剪切(删除或隐藏或缩小)Material dateRangePicker(Dialog)的header_title?

我想删除顶部的空间

图像

我的风格主题(stackOverFlow 的用户回答在这里

我的代码

fun clickDatePicker() {
        setTheme(R.style.AppTheme_MaterailComponent)

        val local = Locale.KOREA
        Locale.setDefault(local)

        val builder = MaterialDatePicker.Builder.dateRangePicker()
        val picker = builder.build()
        picker.apply {
            addOnPositiveButtonClickListener { selection: Pair<Long, Long>? ->
                // just my other logic
                selection?.first?.let { setYearMonthDate(it) }
                    ?.let { it1 -> firstAndSecondMap.put(0, it1) }
                selection?.second?.let { setYearMonthDate(it) }
                    ?.let { it1 -> firstAndSecondMap.put(1, it1) }
                filterDate()
                binding.duration = 0
            }
            show(supportFragmentManager, "picker")
        }
        
}
Run Code Online (Sandbox Code Playgroud)

android datepicker android-datepicker material-design material-components-android

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

如何为工具栏制作圆角?

我看过其他答案,它们都适用于操作栏而不是工具栏,并且它们需要创建新形状。

有没有一种简单的方法可以使用 来做到这一点styles.xml

android android-layout android-toolbar android-shapedrawable material-components-android

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

我需要添加什么依赖项才能使用 com.google.android.material.slider.Slider

我需要向 Android Studio 项目添加什么依赖项才能使用此组件?一般来说,如何知道我需要添加什么依赖项才能使用 Google Material 组件?

android material-design material-components material-components-android android-slider

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

我需要在右上角的材料芯片上显示徽章。我怎样才能轻松做到呢?

我正在尝试这种方式

    override fun draw(canvas: Canvas) {
        super.draw(canvas)
        val viewWidth = width.toFloat()
        val padding = chipEndPadding
        canvas.drawOval(viewWidth - padding, padding, viewWidth, padding + padding, paint)
    }
Run Code Online (Sandbox Code Playgroud)

但它似乎没有出现,可能是因为chipDrawable。任何方法都可以实现这一点。此方法适用于所有其他视图,但不适用于芯片!

android android-chips material-components material-components-android

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

如何在android中的TextInputLayout中对齐文本?

RelativeLayout我有一个TextInputLayout地方EditText可以输入电话号码,还有Textview一个LinearLayout地方可以显示国家/地区代码。当我们开始输入“TextInputLayout”时,滑动到顶部,但我希望它滑动到如图 3 所示的位置。

我遇到了这方面的问题有人可以帮助我吗?这是我的代码:

<RelativeLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                >
        
                                <LinearLayout
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_alignStart="@+id/error_mobile_no"
                                    android:layout_alignTop="@+id/error_mobile_no"
                                    android:layout_alignBottom="@+id/error_mobile_no"
                                    android:layout_marginTop="0dp"
                                    android:layout_marginBottom="0dp"
                                    android:gravity="center"
                                    android:orientation="horizontal">
        
                                    <TextView
                                        android:id="@+id/country_code_textview"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:fontFamily="@font/lorin_regular"
                                        android:text="+971"
                                        android:layout_marginBottom="2dp"
                                        android:textColor="@color/black"
                                        android:textSize="14sp" />
                                </LinearLayout>
        
                                <com.google.android.material.textfield.TextInputLayout
                                    android:id="@+id/error_mobile_no"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_marginEnd="20dp"
                                    android:layout_marginStart="20dp"
                                    app:errorEnabled="true">
        
                                    <EditText
                                        android:id="@+id/etPhoneNumberDetailedActivity"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_gravity="start"
                                        android:autofillHints=""
                                        android:paddingStart="35dp"
                                        android:backgroundTint="#6283a3"
                                        android:fontFamily="@font/lorin_regular"
                                        android:hint="@string/mobile_number"
                                        android:inputType="phone"
                                        android:textColor="@color/black"
                                        android:textColorHint="#6283a3" android:textSize="14sp" />
                                </com.google.android.material.textfield.TextInputLayout>
                            </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是输入文本之前的字段图像。在此输入图像描述

这是输入文字后的图像。在此输入图像描述

但我正在努力让它看起来像这样。 在此输入图像描述

android android-textinputlayout material-components material-components-android

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