【问题标题】:Codename One - Android Material Chip controls in simple layout (or equivalent)代号一 - 简单布局(或等效)的 Android Material Chip 控件
【发布时间】:2020-07-13 09:42:03
【问题描述】:

My Codename One 应用必须具备 Android Chip 控件或同等功能。

它们用在两个地方,布局是:

-第一名

<com.google.android.material.chip.ChipGroup android:id="@+id/chip_group" android:layout_width="wrap_content" android:layout_height="wrap_content">
<com.google.android.material.chip.Chip android:text="@string/first_chip_title" android:id="@+id/first_chip" android:layout_width="match_parent" android:layout_height="wrap_content" app:closeIconEnabled="false"/>
<com.google.android.material.chip.Chip android:text="@string/second_chip_title" android:id="@+id/second_chip" android:layout_width="match_parent" android:layout_height="wrap_content" app:closeIconEnabled="false"/>
...
...
</com.google.android.material.chip.ChipGroup>

-第二名

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<androidx.recyclerview.widget.RecyclerView 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:id="@+id/chip_list" 
...
...
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="0dp" android:layout_marginRight="0dp" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"  tools:listitem="@layout/content"/>
</LinearLayout>

内容(它是 RecyclerView 的元素)在哪里:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content">
<com.google.android.material.chip.Chip android:id="@+id/chip_element" android:layout_width="wrap_content" android:layout_height="wrap_content" app:closeIconEnabled="false" android:textAllCaps="false" android:layout_gravity="center_vertical" style="@style/Widget.MaterialComponents.Chip.Filter" android:layout_weight="1" />
</LinearLayout>

(一些布局参数包含在代码中,尽管它们在这里并不重要)

这是有效的布局(示例是第二种布局,第一种只是一个更简单的版本):

代号一中的等价物是什么?

【问题讨论】:

  • 你能提供一个澄清的截图吗?很难将 XML 可视化。
  • @Shai Almog 是的,你现在可以看到了。

标签: android-layout android-recyclerview android-linearlayout codenameone android-chips


【解决方案1】:

流式布局可以无缝地打破线条。这是Container 的默认布局。我可以很容易地使用标签或按钮来构建类似的东西。请注意,我在这里没有使用 UIID,您需要使用它来添加边距/填充,因此芯片彼此之间更加不同:

    Container chips = new Container();
    // note you can use Button to make them clickable and UIID to style
    // them any way you want
    chips.add(new Label("a very long label for a chip"));
    
    // I can check the chip manually
    Label checked = new Label("another label");
    checked.setMaterialIcon(FontImage.MATERIAL_CHECK);
    checked.setTextPosition(RIGHT);
    checked.getAllStyles().setBorder(RoundBorder.create()
            .rectangle(true)
            .color(0xaaaaaa));
    chips.add(checked);
    chips.add(new Label("chip label 2"));
    chips.add(new Label("chip label 3"));
    chips.add(new Label("chip label 4"));
    chips.add(new Label("chip label 5"));
    chips.add(new Label("short label"));
    chips.add(new Label("very long label for chip"));
    hi.add(chips);

【讨论】:

  • Android Chip 处理检查-取消检查状态,但您的建议作为起点很有用。我创建了一个“芯片”按钮类,我需要知道如何为按钮上的未选中状态设置空白图标空间。怎么办?
  • 您可以在Label 上设置间隙,这将影响子类或定义主题常量:labelGap,以毫米为单位。
  • 我用 gapValue=getIcon().getWidth(); 尝试了你的建议;对于未检查的状态,但文本也会向相反的方向翻译,例如后坐。然后我也尝试了许多价值观和负面价值观。它不像你想要的那样工作。我试过 this.setMaterialIcon(FontImage.MATERIAL_CHECK,0);对于未选中状态和 this.setMaterialIcon(FontImage.MATERIAL_CHECK);对于选中状态,但它们是相同的:图标永远存在。我认为禁用图标的选项缺失,但应该引入。
  • 你说的是图标和文字之间的差距吗?这是在图标和文本之间全局确定的(而不是在两个单独的组件之间)。在未选中状态下,图标宽度将为 0,所以我不确定您要完成什么
  • 如果我输入 0,图标不会关闭,而是显示,而如果我输入 2,图标会变小。如您所见,逻辑是颠倒的,我需要当状态为“未选中”时图标可以消失。怎么办?
猜你喜欢
  • 1970-01-01
  • 2020-03-27
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
相关资源
最近更新 更多