【问题标题】:How to implement a stackoverflow like tag in android textview? [closed]如何在android textview中实现类似stackoverflow的标签? [关闭]
【发布时间】:2019-05-16 09:25:30
【问题描述】:

我正在开发一个食品订购应用程序,用户可以在其中从列表视图中选择所需的配料。

我希望所选项目显示在 stackoverflow 中,例如流行标签 fashion

在图片中,我在stackoverflow应用中选择了java、C#和android,它们显示为独立的盒装项目。

我想在没有 X 的情况下实现相同的字符串框。

我有一个文本视图,我尝试将它们作为独立项添加,但不知道如何实现。

任何帮助都会非常有用。

【问题讨论】:

  • 请添加您的代码以了解您到目前为止所做的工作。
  • 您可以在其中创建一个文本视图,该文本视图动态添加线性布局。文本视图宽度是 wrapcontent,然后你会得到所需的输出
  • 请转至help center 了解如何/在这里询问什么。只是放弃“这就是我想要的”要求是不受欢迎的。当您自己尝试某件事时,遇到特定问题时,我们很乐意提供帮助。但请理解,这个地方并非旨在为您提供从愿景到工作计划可能需要的许多步骤的指导。
  • 可以用android新素材Chips查看。查看this

标签: java android textview tags


【解决方案1】:

使用Chip 构建的最佳方式,请查看下面的一些代码 sn-p。

在您的 .xml 中添加以下代码

<com.google.android.material.chip.ChipGroup
    android:id="@+id/filter_chip_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/filter_chip_txt">

    <com.google.android.material.chip.Chip
        android:id="@+id/filter_chip"
        style="@style/Widget.MaterialComponents.Chip.Filter"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:text="Dogs"/>

    <com.google.android.material.chip.Chip
        android:id="@+id/filter_chip2"
        style="@style/Widget.MaterialComponents.Chip.Filter"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:text="Cats"/>
</com.google.android.material.chip.ChipGroup>

在你的 .java 中

 ChipGroup filterChipGroup = findViewById(R.id.filter_chip_group);
    Chip filterChip = findViewById(R.id.filter_chip);
    Chip filterChip2 = findViewById(R.id.filter_chip2);
    CompoundButton.OnCheckedChangeListener filterChipListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.i(TAG, buttonView.getId() + "");
        }
    };
    filterChip.setOnCheckedChangeListener(filterChipListener);
    filterChip2.setOnCheckedChangeListener(filterChipListener);

我希望这将有助于实现您的期望。

【讨论】:

  • 谢谢大哥!!,只是一个小疑问,如何确保我们在布局中添加的边距在所有设备中保持相同?例如 marginTop = 200dp,在不同的设备中,UI看起来不一样!,请对此有任何想法?
  • 这是一个不确定的情况,所有设备都有不同的分辨率,但请查看this,可能会对您有所帮助
  • 谢谢老兄!!关于筹码的一个问题,有没有办法可以让所有筹码向右移动,并且它们之间的差距更小?
  • 你能看看这个链接吗:stackoverflow.com/questions/56174608/…
猜你喜欢
  • 2011-05-24
  • 2016-03-12
  • 2015-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
  • 2016-02-17
  • 1970-01-01
相关资源
最近更新 更多