【问题标题】:Android Chip Custom Height Or Padding - Chipgroup spacingAndroid 芯片自定义高度或填充 - 芯片组间距
【发布时间】:2019-01-14 09:06:53
【问题描述】:

有没有办法改变默认材料芯片垂直填充或高度?从docs 我看到肯定有一种方法可以设置最小高度,但我想让芯片“更薄”,就像这个设计一样:

【问题讨论】:

    标签: android material-design material-components-android material-components android-chips


    【解决方案1】:

    在 1.1.0-alpha* 版本中,为芯片添加了额外的间距。经过大量的尝试和尝试,我设法使用以下方法消除了该间距:

    app:chipMinTouchTargetSize="0dp"
    

    看Chip的class文件,好像和Android的最小触摸目标尺寸有关,改之前考虑一下。

    【讨论】:

    • 谢谢!这太烦人和令人费解了。
    • 另外,可以通过在芯片上以编程方式调用 setEnsureMinTouchTargetSize(false) 来实现此行为。
    • 令人难以置信的是,Android 创作者可以在这样的名称下隐藏边距设置。谢谢!
    • 非常感谢!经过大量搜索,您的解决方案终于奏效了。
    • 更新:只需要 app:chipMinHeight="{size}" 来改变芯片的大小。
    【解决方案2】:

    以下是填充的芯片属性。希望对你有帮助

    填充

    app:chipStartPadding
    app:iconStartPadding
    app:iconEndPadding
    app:textStartPadding
    app:textEndPadding
    app:closeIconStartPadding
    app:closeIconEndPadding
    app:chipEndPadding
    

    更多信息:Click here

    【讨论】:

    • 对不起,我可能不太清楚,我正在寻找 textBottomPadding 和 textTopPadding 或 chipHeight 行中的东西 :) 或一些肮脏的黑客来实现它。
    • 最好借助drawable来定制自己的芯片。
    • 是的,这就是我可能最终会做的事情,只是想先确保我没有遗漏任何东西。
    • 我找到了这个第三方库:code.hootsuite.com/…
    • 如果您想定制,请查看此链接:devexchanges.info/2017/01/…
    【解决方案3】:

    为了让Chip更薄,需要调整两个属性:chip height和text size。

    <style name="ThinnerChip" parent="Widget.MaterialComponents.Chip.Action">
        <item name="chipCornerRadius">12dp</item>
        <item name="chipMinHeight">24dp</item>
        <item name="android:textAppearance">@style/SmallerText</item>
    </style>
    
    <style name="SmallerText" parent="TextAppearance.AppCompat.Small">
        <item name="android:textSize">11sp</item>
    </style>
    

    然后像往常一样应用样式。

    <com.google.android.material.chip.Chip
        style="@style/ThinnerChip"
        ...
        />
    

    【讨论】:

      【解决方案4】:

      您可以使用自定义样式:

        <style name="MaterialComponents_Chip_Thin" parent="@style/Widget.MaterialComponents.Chip.Entry">
          <item name="chipMinHeight">24dp</item>
          <item name="chipMinTouchTargetSize">24dp</item>
          <item name="chipIconSize">18dp</item>
          <item name="android:textAppearance">@style/TextAppearance.MaterialComponents.Body2_Thin</item>
          <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MaterialComponents.Chip_Thin</item>
        </style>
        <style name="ShapeAppearanceOverlay.MaterialComponents.Chip_Thin" parent="">
          <item name="cornerSize">12dp</item>
        </style>
        <style name="TextAppearance.MaterialComponents.Body2_Thin" parent="TextAppearance.MaterialComponents.Body2">
          <item name="android:textSize">12sp</item>
        </style>
      

      并应用它:

          <com.google.android.material.chip.Chip
              style="@style/MaterialComponents_Chip_Thin"
              .../>
      

      【讨论】:

      • chipMinTouchTargetSize 是我所缺少的。谢谢
      【解决方案5】:

      这为我删除了默认的垂直填充:

      app:ensureMinTouchTargetSize="false"
      

      【讨论】:

        【解决方案6】:

        您必须设置芯片的高度以使其“更薄”。
        检查以下代码:

        build.gradle(应用程序)

        implementation 'com.google.android.material:material:1.0.0-rc01'
        

        ic_vector_android.xml

        <vector xmlns:android="http://schemas.android.com/apk/res/android"
                android:width="24dp"
                android:height="24dp"
                android:viewportWidth="24.0"
                android:viewportHeight="24.0">
            <path
                android:fillColor="#FFFFFF"
                android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z"/>
        </vector>
        

        your_layout.xml

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#e3e3e3"
            android:orientation="vertical"
            android:padding="20dp">
        
            <com.google.android.material.chip.ChipGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:chipSpacing="18dp">
        
                <com.google.android.material.chip.Chip
                    style="@style/Widget.MaterialComponents.Chip.Entry"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:checkable="false"
                    android:text="KRAKOW"
                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
                    android:textColor="#636161"
                    app:chipCornerRadius="10dp"
                    app:chipIcon="@drawable/ic_vector_android"
                    app:chipIconSize="14dp"
                    app:chipIconTint="#636161"
                    app:chipStartPadding="8dp"
                    app:chipStrokeColor="#636161"
                    app:chipStrokeWidth="1dp"
                    app:closeIconEnabled="false" />
        
                <com.google.android.material.chip.Chip
                    style="@style/Widget.MaterialComponents.Chip.Entry"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:checkable="false"
                    android:text="Today"
                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
                    android:textColor="#ffffff"
                    app:chipBackgroundColor="#636161"
                    app:chipCornerRadius="10dp"
                    app:closeIconEnabled="false" />
        
                <com.google.android.material.chip.Chip
                    style="@style/Widget.MaterialComponents.Chip.Entry"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:checkable="false"
                    android:text="Tomorrow"
                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
                    android:textColor="#ffffff"
                    app:chipBackgroundColor="#636161"
                    app:chipCornerRadius="10dp"
                    app:chipIcon="@drawable/ic_vector_android"
                    app:chipIconSize="14dp"
                    app:chipStartPadding="8dp"
                    app:closeIconEnabled="false" />
            </com.google.android.material.chip.ChipGroup>
        </LinearLayout>
        

        要获得更薄的芯片,我必须设置以下属性:

        android:layout_height="20dp" // Thinner Height
        app:chipIconSize="14dp"      // Smaller icon size
        app:chipStartPadding="8dp"   // Add padding to start of icon
        app:chipCornerRadius="10dp"  // Set Corner radius
        

        预览

        【讨论】:

        • 嗨 Viraj,如何以编程方式设置动态芯片的布局高度!!?
        【解决方案7】:

        有一个非常简单的方法可以实现我想要的:我需要做的就是在包含我的芯片的ChipGroup 上设置android:layout_height 属性。

        我想这是有道理的,在单个芯片上实现它是不可能的...... :>

        【讨论】:

        • 如果你只是用“android:layout_height”限制高度,它可以切割芯片。更好的方法是按照@Pramod Garg 的建议将参数“chipMinTouchTargetSize”更改为 0
        【解决方案8】:

        使用 setEnsureMinTouchTargetSize(false) 确保间距最小。

        Chip chip = new Chip(getContext());
            chip.setId(selectedWordGroup.id);
            chip.setText(selectedWordGroup.getName());
            chip.setTag(i);
            chip.setCheckable(true);
            chip.setCheckedIconVisible(false);
            chip.setChecked((mFlashcardSetupActivity.allWordGroups.get(i).getSelected()));
            chip.setClickable(true);
            chip.setFocusable(true);
            chip.setEnsureMinTouchTargetSize(false);
            chip.setChipDrawable(ChipDrawable.createFromAttributes(getContext(), null, 0, R.style.CustomChipChoice));
        
            chip.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean selected) {
                }
            });
            mChipGroupGroups.addView(chip);
        
        
        
        
        <com.google.android.material.chip.ChipGroup
                            android:id="@+id/chip_group_groups"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:padding="4dp"
                            card_view:chipSpacing="4dp"
                        >
        

        【讨论】:

          【解决方案9】:

          使用java代码:

          int paddingDp = (int) TypedValue.applyDimension(
                          TypedValue.COMPLEX_UNIT_DIP, 10,
                          getResources().getDisplayMetrics()); 
          
                  mChip.setPadding(paddingDp, 0, paddingDp, 0);
          

          输出:

          【讨论】:

            【解决方案10】:

            如果将多行 Chip 动态添加到 ChipGroup 直到它扩展到多行,则垂直边距为零。 在 ChipGroup 上设置 app:chipSpacingVertical 以启用边距。

            <com.google.android.material.chip.ChipGroup
                android:id="@+id/mainTagChipGroup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:chipSpacingVertical="4dp"
                />
            

            注意:您也可以设置 app:chipSpacingHorizo​​ntal 或 app:chipSpacing。 注意:奇怪的是,在 xml 中定义的静态 Chip 与以编程方式添加的动态 Chip 的默认边距是不同的。我正在使用 com.google.android.material:material:1.1.0-alpha04。 注意:在芯片上设置 layoutParams 不起作用。

            【讨论】:

              【解决方案11】:
               //need or other way put parent ChipGroup LinearLayout and set h*W  
                //and  also width and height
              
               <com.google.android.material.chip.Chipandroid 
                  android:paddingLeft="10dp" 
                  android:paddingRight="10dp" 
                  android:layout_width="10dp" 
                  android:layout_height="10dp" 
                  app:mcv_textColor="@color/white" /> 
              

              【讨论】:

              • 这会因不同的字体大小而中断
              • @rishabh deep Singh 你需要在不同尺寸的布局中设置尺寸 xdpi,mdpi,xxdpi
              【解决方案12】:

              app:chipStartPadding="@dimen/_10sdp"

              【讨论】:

                猜你喜欢
                • 2020-08-25
                • 1970-01-01
                • 2020-09-02
                • 2018-12-14
                • 1970-01-01
                • 2021-01-26
                • 2021-06-24
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多