android:无法制作多行芯片组 [英] android:unable to make multiline chipgroup

查看:107
本文介绍了android:无法制作多行芯片组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在相对布局中有一个芯片组,以及一个文本视图,其代码如下所示.

I have a chipgroup within a relative layout along with a textview whose code is shown below.

    <RelativeLayout
    ...

    <TextView
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:text="TextTitle"
        android:layout_alignParentStart="true"
        android:gravity="left"
        android:layout_marginTop="16dp"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="14sp"
        android:id="@+id/tv"
        android:layout_toLeftOf="@id/cg"
        android:layout_alignBaseline="@id/cg"
        />

       <com.google.android.material.chip.ChipGroup
       android:id="@+id/cg"
       android:layout_width="wrap_content"
       android:layout_height="14dp"
       android:layout_marginTop="20dp"
       android:layout_marginBottom="5dp"
       android:layout_alignParentRight="true"
       android:layout_alignParentEnd="true"
       android:textColor="@android:color/black"
       android:textStyle="bold"
       android:textSize="12sp"
       app:singleSelection="false"
       app:chipSpacingVertical="32dp"
       app:chipSpacingHorizontal="5dp"
       app:singleLine="false"
       />
  </RelativeLayout>

我像下面这样动态地向该芯片组添加芯片:

I add chips to this chipgroup dynamically like the below:

Chip chip;

for(int i=0;i<2;i++){
    chip = new Chip(this);
    chip.setText("Text:"+i);
    chip.setChipBackgroundColorResource(android.R.color.darker_gray);
    chip.setTextColor(Color.WHITE);
    mChipGroup.addView(chip);
}

以上内容将文本视图和芯片组并排放置(芯片在文本视图的最右边,这就是我想要的).

The above brings the textview and chipgroup side by side (with the chips at the extreme right from the textview,which is what I want).

对于2或3芯片,输出如下:

但是当我有3个以上的芯片时,它看起来像这样:

我参考了文档,试图使芯片组成为多行,并在芯片组xml中添加了以下内容

I referred the docs and tried to make the chipgroup multiline and added the following in chipgroup xml

   app:chipSpacingVertical="32dp"
   app:chipSpacingHorizontal="5dp"
   app:singleLine="false"

它看起来与pic2中的一样.

It looks the same as in the pic2.

我需要的是一个带有多行选项的芯片组,其间距与pic1(即pic1)中的间距相同,如果芯片数增加,我希望Text3在Text0下,Text4在Text1下,依此类推.

.

我确定我一定会丢失一些东西,但是不知道它是什么.任何帮助都将非常有用.谢谢!!

Im sure I must be missing something,but dont know what it is.Any help will be very useful.Thanks in advance!!

推荐答案

我从@Sajith的答案中进行了更新,看看它是否可以工作:

I update from @Sajith 's answer,see if it could work:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="25"
            android:layout_height="wrap_content"
            android:text="TextTitle"
            android:gravity="left"
            android:layout_marginTop="16dp"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="14sp"
            android:id="@+id/tv" />

        <FrameLayout
            android:layout_width="0dp"
            android:layout_weight="75"
            android:layout_height="wrap_content" >

            <com.google.android.material.chip.ChipGroup
                android:id="@+id/cg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="5dp"
                android:textColor="@android:color/black"
                android:textStyle="bold"
                android:textSize="12sp"
                app:singleSelection="false"
                app:chipSpacingVertical="32dp"
                app:chipSpacingHorizontal="5dp"
                app:singleLine="false" />
        </FrameLayout>

    </LinearLayout>
</RelativeLayout>

这篇关于android:无法制作多行芯片组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆