【问题标题】:how to align android chips to the end in chipgroup?如何将 android 芯片与芯片组中的末尾对齐?
【发布时间】:2019-05-16 18:29:25
【问题描述】:

您好,我有一个芯片组,我动态创建芯片并添加到芯片组。

但是我希望芯片在芯片组的右端,但它总是在左边,如图所示。

我希望芯片 Text:0 和 Text:1 位于最右边。

这是我的代码:

 <RelativeLayout
     ....
         >

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextTitle"
        android:layout_alignBaseline="@id/chipGroup"
        android:gravity="end|bottom"
        android:layout_marginTop="16dp"
        android:layout_below="@id/toptv"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="14sp"
        android:id="@+id/tt1"
        />

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_toRightOf="@id/tt1"
        android:layout_below="@id/toptv"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:textSize="12sp"
        />
     </RelativeLayout>

这就是我将筹码动态添加到筹码组的方式:

    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);
    }

我搜索了某些 SOF 帖子,但无法将芯片对齐到右侧。任何帮助都会非常有帮助。

【问题讨论】:

  • 通过绘图添加您期望的输出图像以了解场景
  • 我期望的是“Text0”和“Text1”应该在最右边,而不是非常靠近“TextTitle”文本视图!

标签: java android android-chips


【解决方案1】:

我用过它,它对我有用

添加这一行: android:layoutDirection="rtl"

        <com.google.android.material.chip.ChipGroup
            android:id="@+id/questionLayout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layoutDirection="rtl"
            >


        </com.google.android.material.chip.ChipGroup>

【讨论】:

    【解决方案2】:

    您需要更改您的ChipGroup 组属性。这是更改的代码。

    <RelativeLayout
     ....
         >
    
    <TextView
    
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextTitle"
        android:layout_alignBaseline="@id/chipGroup"
        android:gravity="end|bottom"
        android:layout_marginTop="16dp"
        android:layout_below="@id/toptv"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="14sp"
        android:id="@+id/tt1"
        />
    
    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_toRightOf="@id/tt1"
        android:layout_below="@id/toptv"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:gravity="end"
        android:textSize="12sp"
        />
     </RelativeLayout>
    

    我希望这会奏效。如果没有,请在您的问题上发布您的RelativeLayout

    【讨论】:

    • 感谢 Mehul 的回答。相对布局只包含 layout_height 和 width 作为 match_parent.btw,上面的代码没有将它们对齐!
    • 所以使用linearLayout并使用weightsum标签和权重,这样你就可以实现你想要的
    【解决方案3】:

    替换它

     <RelativeLayout
     ....
         >
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextTitle"
        android:layout_alignBaseline="@+id/chipGroup"
        android:layout_toLeftOf="@+id/chipGroup"
        android:gravity="end|bottom"
        android:layout_marginTop="16dp"
        android:layout_below="@+id/toptv"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="14sp"
        android:id="@+id/tt1"
        />
    
    <com.google.android.material.chip.ChipGroup
        android:id="@+id/chipGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/toptv"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:textSize="12sp"
        />
     </RelativeLayout>
    

    【讨论】:

    • 我刚刚将 textview 的重力设置在上面,它可以工作。但是有一个问题,每当我向芯片组添加更多芯片时,它会缩小左侧的文本视图并且看起来很糟糕,你能建议一些修复它吗!!
    【解决方案4】:

    您可以将 scaleX=-1 添加到您的芯片组中,并且您的芯片应该有 scaleX=-1

    【讨论】:

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