使用setText()时芯片文本是重叠图标

问题描述 投票:0回答:2

我正在使用android.support.design.chip.Chip,我得到的问题是,当我通过setText(“...”)动态设置文本时,通过重叠图标错误地显示文本,但是当我将其设置为作为app的xml文件: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文本)

android android-widget settext android-chips
2个回答
0
投票

代替

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


0
投票

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

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

代替:

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

然后,解决方案是使用Chip方法setChipText(CharSequence文本)

© www.soinside.com 2019 - 2024. All rights reserved.