【问题标题】:Chip text is overlapping icon when using setText()使用 setText() 时,芯片文本重叠图标
【发布时间】:2019-03-05 11:27:57
【问题描述】:

我正在使用 android.support.design.chip.Chip,我遇到的问题是,当我通过 setText("...") 动态设置文本时>,文本通过重叠图标错误地显示,但是当我在 xml 文件中将其设置为 app:chipText="Hello" 时,它显示正确。

这里有我的代码:

activity.java

        Chip x= new Chip(mView);
    x.setChipDrawable(ChipDrawable.createFromResource(mView, R.xml.chip_style));

chip_style.xml

<?xml version="1.0" encoding="utf-8"?>
<chip xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/Widget.MaterialComponents.Chip.Entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="@style/TextAppearance.MaterialComponents.Chip"
    app:chipIcon="@drawable/ic_person"/>

---------解决方案----------

那么,解决方案就是使用Chip方法setChipText(CharSequence text)

【问题讨论】:

    标签: android android-widget settext android-chips


    【解决方案1】:

    而不是

    android.support.design.chip.Chip

    使用

    实现'com.google.android.material:material:1.0.0-beta01'

    <com.google.android.material.chip.Chip
        style="@style/Widget.MaterialComponents.Chip.Entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chip_text"
        app:chipIcon="@drawable/ic_avatar_circle_24"/>
    

    动态

    val chip = Chip(context)
    chip.text = "Name Surname"
    chip.chipIcon = ContextCompat.getDrawable(requireContext(), baseline_person_black_18)
    chip.isCloseIconEnabled = true
    // following lines are for the demo
    chip.setChipIconTintResource(R.color.chipIconTint)
    chip.isClickable = true
    chip.isCheckable = false
    chipGroup.addView(chip as View)
    chip.setOnCloseIconClickListener { chipGroup.removeView(chip as View) }
    

    详细参考:https://medium.com/material-design-in-action/chips-material-components-for-android-46001664a40f

    【讨论】:

    • 问题是Material Design建议不要同时使用这两个库,其他UI元素我需要android.support.design
    • 是的,你是对的。如果是这种情况,请首先确认您使用的是设计库中的 Chip 类,完整名称是 android.support.design.chip.Chip。
    • 当我使用 setText(..) 设置文本时似乎会发生错误,但如果我在 xml 文件中默认设置它,则会正确显示...
    【解决方案2】:

    我终于注意到了我的错误,我正在使用:

    trabajador.setText(FunctionsUtil.safeCursorGetValue(trabajadorData, KEY_NOMBRE));
    

    代替:

    trabajador.setChipText(FunctionsUtil.safeCursorGetValue(trabajadorData, KEY_NOMBRE));
    

    然后,解决方法是使用Chip方法setChipText(CharSequence text)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 2017-09-09
      • 2021-09-20
      • 1970-01-01
      相关资源
      最近更新 更多