标签: material-components-android

在android中动态创建Choice Chip

我正在使用 Material Components 来创建 Choice 芯片。我遵循了https://material.io/develop/android/components/chip/文档。有足够的东西可以用 XML 创建芯片,但不知道如何以编程方式创建选择芯片。

我使用以下代码动态创建芯片,但默认情况下它创建动作芯片。

val chip = Chip(activity)
chip.text = ("Chip 1")
chipGpRow.addView(chip)
Run Code Online (Sandbox Code Playgroud)

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

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

MaterialButtonToggleGroup 上需要单选

是否有选项可以MaterialButtonToggleGroup在使用时选择所需的按钮app:singleSelection="true"

当单击不同的按钮工作正常(所有其他按钮都被取消选择),但是当您单击同一个(已选择)按钮时,它会自行取消选择它,我想保持选中状态。

我的例子:

 <com.google.android.material.button.MaterialButtonToggleGroup
      android:layout_width="wrap"
      android:layout_height="wrap_content"
      app:singleSelection="true">

      <com.google.android.material.button.MaterialButton
        android:id="@+id/filterA"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A"/>

      <com.google.android.material.button.MaterialButton
        android:id="@+id/filterB"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B"/>

      <com.google.android.material.button.MaterialButton
        android:id="@+id/filterC"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="C"/>

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

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

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

如何设置浮动操作按钮图像以填充按钮?

通常图标设置为浮动动作按钮,但我需要设置图像,所以我可以制作圆形图像.

我将它添加到项目中(使用Android Studio-> New - > Image Asset),但图像没有填满整个按钮:

在此输入图像描述

我的xml:

<com.github.clans.fab.FloatingActionMenu
    android:id="@+id/run_menu"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:paddingBottom="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:visibility="visible"
    fab:fab_colorNormal="#DA4336"
    fab:fab_colorPressed="#E75043"
    fab:fab_colorRipple="#99FFFFFF"
    fab:fab_shadowColor="#66000000"
    fab:fab_showShadow="true"
    fab:layout_constraintBottom_toBottomOf="parent"
    fab:layout_constraintHorizontal_bias="0.0"
    fab:layout_constraintLeft_toLeftOf="parent"
    fab:layout_constraintRight_toRightOf="parent"
    fab:layout_constraintTop_toTopOf="parent"
    fab:layout_constraintVertical_bias="0.0"
    fab:menu_backgroundColor="#ccffffff"
    fab:menu_fab_label="Choose an action"
    fab:menu_labels_colorNormal="#333333"
    fab:menu_labels_colorPressed="#444444"
    fab:menu_labels_colorRipple="#66FFFFFF"
    fab:menu_labels_ellipsize="end"
    fab:menu_labels_maxLines="-1"
    fab:menu_labels_position="left"
    fab:menu_labels_showShadow="true"
    fab:menu_labels_singleLine="true"
    fab:menu_openDirection="up"
    android:layout_height="0dp"
    android:layout_width="0dp">

    <com.github.clans.fab.FloatingActionButton
        android:id="@+id/shop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/store"
        fab:fab_label="Store" />

</com.github.clans.fab.FloatingActionMenu>
Run Code Online (Sandbox Code Playgroud)

关于如何修复它的任何想法?应该如何正确制作?

android android-button material-design floating-action-button material-components-android

9
推荐指数
2
解决办法
4617
查看次数

如何使 TextInputEditText 只读?

我正在尝试将 TextInputEditText 设为只读,但我的光标快速闪烁并触发了单击。

我尝试设置isFocusable = falseisClickable = false.

XML 布局:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/label_address"
    android:layout_marginTop="@dimen/material_layout_vertical_spacing_between_content_areas"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/input_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionNext"/>

</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

使只读的代码:

fun TextInputEditText.disable() {
    isFocusable = false
    isClickable = false
}
Run Code Online (Sandbox Code Playgroud)

使 TextInputEditText 只读的正确方法或正确方法是什么?

android kotlin material-design material-components-android

9
推荐指数
2
解决办法
6691
查看次数

如何更改 TextInputLayout 中密码切换的颜色?

TextInputLayout如果EditText聚焦与否,我需要更改密码切换的颜色。我已经这样做了,但它不起作用。颜色总是等于浅灰色(从状态聚焦 = 假)

布局

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:passwordToggleEnabled="true"
        app:passwordToggleDrawable="@drawable/password_toggle_selector"
        app:passwordToggleTint="@color/color_password_toggle">

    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

color_password_toggle

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/color_green" android:state_checked="true"  />
    <item android:color="@color/color_grey" android:state_focused="true" />
    <item android:color="@color/color_light_grey" android:state_focused="false" />
Run Code Online (Sandbox Code Playgroud)

password_toggle_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_eye" android:state_checked="true />
    <item android:drawable="@drawable/ic_eye_off" />
Run Code Online (Sandbox Code Playgroud)

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

9
推荐指数
3
解决办法
6913
查看次数

如何从ChipGroup获取精选芯片?

我在互联网上搜索了很多,但找不到确切的解决方案。这是我上次从 SO 尝试的链接。 从 ChipGroup 中获取选定的 Chips

我想在单击按钮时从芯片组中获取选定的芯片。

此函数包含在 RecyclerView 中显示名称的信息

private void getNames() {
    List<String> names = Arrays.asList(getResources().getStringArray(R.array.names));
    int count = 0;
    for ( String name : names){
        list.add(new Names(name));
        count++;
    }
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
    namesAdapter = new NamesAdapter(MainActivity.this, list);
    recyclerView.setAdapter(namesAdapter);
}
Run Code Online (Sandbox Code Playgroud)

当单击 RecyclerView 项时,将一个芯片添加到 ChipGroup 中,这是功能

public void onItemSelected(Names name) {
    Chip chip = new Chip(this);
    chip.setText(name.getName());
    chip.setCloseIconVisible(true);
    chip.setCheckable(false);
    chip.setClickable(false);
    chip.setOnCloseIconClickListener(this);
    chipGroup.addView(chip);
    chipGroup.setVisibility(View.VISIBLE);
}
Run Code Online (Sandbox Code Playgroud)

这是ChipGroup展示芯片的图片

从 ChipGroup 获取值的函数

public void getChipGroupValues(){
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View …
Run Code Online (Sandbox Code Playgroud)

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

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

带有 MaterialComponents 主题的 ActionBar 背景

我想自定义我的 ActionBar。我的主题如下所示:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@style/ThemeOverlay.MaterialComponents.ActionBar">
    <item name="background">@drawable/actionBarBackground</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在值文件夹中:

<drawable name="actionBarBackground">#FFFFFFFF</drawable>
Run Code Online (Sandbox Code Playgroud)

在 values-night 文件夹中:

<drawable name="actionBarBackground">#FF000000</drawable>
Run Code Online (Sandbox Code Playgroud)

不知道为什么我的 ActionBar 背景颜色没有相应改变。我也尝试以其他不同的方式更改我的主题,但没有任何效果。这是我的其他尝试:

相反actionBarStyle,我使用了actionBarTheme.

<item name="actionBarTheme">@style/MyActionBar</item>
Run Code Online (Sandbox Code Playgroud)

我也尝试使用colorPrimary.

<item name="colorPrimary">@color/actionBarBackground</item>
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

android android-layout android-actionbar material-components material-components-android

9
推荐指数
3
解决办法
4462
查看次数

MaterialDatePicker 获取选定的日期

我在 Android 中像这样调用 MaterialDatePicker:

MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();

CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
builder.setCalendarConstraints(constraintsBuilder.build());

int dialogTheme = resolveOrThrow(getContext(), R.attr.materialCalendarTheme);
builder.setTheme(dialogTheme);

MaterialDatePicker<?> picker = builder.build();

picker.show(getFragmentManager(), picker.toString());
Run Code Online (Sandbox Code Playgroud)

图书馆是:

dependencies {
    implementation 'com.google.android.material:material:1.2.0-alpha01'
}
Run Code Online (Sandbox Code Playgroud)

如何获取此日历的选定日期?我找不到任何听众喜欢onDateSetOnDateSetListener

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

9
推荐指数
3
解决办法
8948
查看次数

colorAccent 或 colorSecondary 与 Material Component 主题

使用 Android Studio 向导创建新应用程序时,使用派生自的主题Theme.MaterialComponents.Light.DarkActionBarcolorAccent设置 a。另一方面,官方文档使用colorSecondary并且根本没有提及colorAccent。两者可以互换吗?更喜欢什么?

android android-theme material-components material-components-android

9
推荐指数
2
解决办法
3260
查看次数

什么时候使用 MaterialToolbar 和 androidx Toolbar 以及两者的区别?

当我切换到AndroidX 时,我发现它Toolbar已经转移到androidx.appcompat.widget.Toolbar还有一个新的MaterialToolbar引入,因为com.google.android.material.appbar.MaterialToolbar 这两者之间有什么区别以及何时使用它们?

android material-design android-toolbar material-components-android androidx

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