簡體   English   中英

以編程方式更改芯片小部件樣式不起作用 - Android

[英]Change Chip Widget style programmatically not working - Android

我正在用 Chips 做一個清單。 我希望可以選擇這個芯片,所以,看看https://material.io/develop/android/components/chip/我看到我可以有一個“選擇芯片”。

由於我需要動態創建和添加,我必須配置特定的顏色、顏色波紋、...

所以我必須配置它的是:

val chip = Chip(context, null, R.style.CustomChipChoice)
            chip.isClickable = true
            chip.isCheckable = true
            chip.isCheckedIconVisible=false
            chip.height = ScreenUtils.dpToPx(40)
            chip.chipCornerRadius = (ScreenUtils.dpToPx(20)).toFloat()
            chip.chipStrokeWidth = (ScreenUtils.dpToPx(2)).toFloat()
            chip.setTextAppearanceResource(R.style.ChipTextStyle)
            return chip

我用R.style.CustomChipChoice嘗試的是:

CustomChipChoice 樣式

<style name="CustomChipChoice" parent="@style/Widget.MaterialComponents.Chip.Choice">
        <item name="chipBackgroundColor">@color/background_color_chip_state_list</item>
        <item name="chipStrokeColor">@color/background_color_chip_state_list</item>
        <item name="rippleColor">@color/topic_social_pressed</item>
</style>

background_color_chip_state_list

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/topic_social_selected" android:state_checked="true" />
    <item android:color="@color/topic_social_pressed" android:state_pressed="true" />
    <item android:color="@color/topic_unselected_background" />
</selector>

stroke_color_chip_state_list

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/topic_social_pressed" android:state_checked="true"/>
    <item android:color="@color/grey_material2" android:state_checked="false"/>
</selector>

如您所見,我制作了可點擊和可檢查的芯片(隱藏了我不需要的檢查圖標)。

但是當我測試它時,顏色沒有設置。 芯片只是看起來像默認顏色(灰色的比例)

我可以在哪里申請或如何申請這種自定義樣式?

PS:

我做了一個快速測試,看看我的 CustomStyle 是否格式錯誤/等等。

我通過 xml 添加了一個視圖並且工作得很好......

<android.support.design.chip.Chip
                android:id="@+id/test"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/CustomChipChoice"
                android:checkable="true"
                android:clickable="true"
                app:checkedIconVisible="false"
                android:text="Chip Test"/>

不能使用構造函數val chip = Chip(context, null, R.style.CustomChipChoice)因為第三個參數不是樣式而是主題中的屬性R.attr.chipStyle
Chip沒有像其他組件那樣具有 4 個參數的構造函數,因為它擴展了不支持 4 個參數構造函數的AppCompatCheckbox

但是,您可以使用不同的東西。
第一個選項

只需使用xml 布局( single_chip_layout.xml ) 以您喜歡的樣式定義單個Chip

<com.google.android.material.chip.Chip
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/CustomChipChoice"
    ...
/>

<style name="CustomChipChoice" parent="@style/Widget.MaterialComponents.Chip.Choice">
...
</style>

然后代替val chip = Chip(context, null, R.style.CustomChipChoice)使用:

val chip = layoutInflater.inflate(R.layout.single_chip_layout, chipGroup, false) as Chip

在Java中:

Chip chip =
          (Chip) getLayoutInflater().inflate(R.layout.single_chip_layout, chipGroup, false);

第二個選項

另一種選擇是使用setChipDrawable方法覆蓋ChipDrawable內部Chip

  Chip chip = new Chip(this);
  ChipDrawable chipDrawable = ChipDrawable.createFromAttributes(this,
      null,
      0,
      R.style.Widget_MaterialComponents_Chip_Choice);
  chip.setChipDrawable(chipDrawable);

為了在代碼中設置芯片樣式,您可以嘗試以下操作:

val chip = Chip(context)
val drawable = ChipDrawable.createFromAttributes(context, null, 0, R.style.Widget_MaterialComponents_Chip_Choice)
chip.setChipDrawable(drawable)

CustomChipChoice不是一種樣式,它只是對樣式的引用。 因此將R.style.CustomChipChoice更改為: R.attr.CustomChipChoice

val newChip = Chip(context, null, R.attr.CustomChipChoice)

但在此之前,您應該在項目的values.xml文件中添加此CustomChipChoice 為了這。 如果您的項目沒有values.xml ,請在values目錄中創建它。

然后像這樣添加CustomChipChoice

值.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="CustomChipChoice" format="reference" />
</resources>

現在在styles.xml添加您的樣式。

樣式文件

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        .
        .
        <item name="CustomChipChoice">@style/CustomChipChoiceStyle</item>
        .
        .
</style>

現在CustomChipChoice attr 引用了此樣式,現在您可以在styles.xml文件中創建自定義樣式。

樣式文件

<style name="CustomChipChoiceStyle" parent="@style/Widget.MaterialComponents.Chip.Action">
        .
        <item name="checkedIconVisible">false</item>
        <item name="android:focusable">true</item>
        <item name="android:clickable">true</item>
        <item name="chipBackgroundColor">@color/colorWhite</item>
        <item name="chipIcon">@drawable/ic_filter</item>
        <item name="chipIconVisible">true</item>
        <item name="textStartPadding">0dp</item>
        <item name="textEndPadding">0dp</item>
        .
        .
        <item name="android:textAppearance">@style/ChipTextStyleAppearance</item>
</style>

如果您想更改芯片的文本外觀。 這是ChipTextStyleAppearance 你可以這樣添加。

樣式文件

<style name="ChipTextStyleAppearance">
        <item name="android:fontFamily">@font/main_font</item>
        <item name="android:textSize">13dp</item>
        <item name="android:textColor">#ffffff</item>
</style>

不要忘了加AppThemeandroidManifest.xmlapplicationactivity標簽。

androidManifest.xml

<application
        .
        .
        android:theme="@style/AppTheme">

<activity
            .
            .
            android:theme="@style/AppTheme" />

科特林

xml

    <com.google.android.material.chip.ChipGroup
                android:id="@+id/chipGroup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal">
</com.google.android.material.chip.ChipGroup>

班級

 data class Parametro(
       var idParametro: Long,
       var nombreParametro: String? )

主要的

        listParametro.forEach { it->
                    val chip = Chip(context)
                    chip.id= it.idParametro.toInt()
                    chip.text= it.nombreParametro
                    chip.isClickable = true
                    chip.isCheckable = true
                    chip.setOnCheckedChangeListener { buttonView, isChecked ->
                        Log.i("checkedChipIds","${buttonView.id} $isChecked")
                    }
                    mBinding.chipGroup.addView(chip)
                }

這個對我有用 :)

還有另一種非常簡單的方法。

樣式文件

<style name="Widget.MyApp.Chip" parent="Widget.MaterialComponents.Chip.Choice">
    <item name="android:textAppearance">@style/TextAppearance.MyApp.Chip</item>
    <item name="chipIconTint">?attr/colorPrimary</item>
</style>

主題文件

<style name="Theme.MyApp.MyTheme" parent="Base.Theme.MyApp">
    <item name="chipStyle">@style/Widget.MyApp.Chip</item>
</style>

有了這個,無論活動中應用了主題Theme.MyApp.MyActivity所有芯片,無論芯片是通過 xml 還是以編程方式添加的,都將遵循此自定義樣式。

我正在用Chips做一份清單。 我希望可以選擇此芯片,因此,請看一下https://material.io/develop/android/components/chip/,我可以找到一個“ Choice Chip”。

當我需要動態創建和添加時,我必須配置特定的顏色,顏色波紋,...

所以我要配置的是:

val chip = Chip(context, null, R.style.CustomChipChoice)
            chip.isClickable = true
            chip.isCheckable = true
            chip.isCheckedIconVisible=false
            chip.height = ScreenUtils.dpToPx(40)
            chip.chipCornerRadius = (ScreenUtils.dpToPx(20)).toFloat()
            chip.chipStrokeWidth = (ScreenUtils.dpToPx(2)).toFloat()
            chip.setTextAppearanceResource(R.style.ChipTextStyle)
            return chip

我用R.style.CustomChipChoice嘗試的是:

CustomChipChoice樣式

<style name="CustomChipChoice" parent="@style/Widget.MaterialComponents.Chip.Choice">
        <item name="chipBackgroundColor">@color/background_color_chip_state_list</item>
        <item name="chipStrokeColor">@color/background_color_chip_state_list</item>
        <item name="rippleColor">@color/topic_social_pressed</item>
</style>

background_color_chip_state_list

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/topic_social_selected" android:state_checked="true" />
    <item android:color="@color/topic_social_pressed" android:state_pressed="true" />
    <item android:color="@color/topic_unselected_background" />
</selector>

stroke_color_chip_state_list

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/topic_social_pressed" android:state_checked="true"/>
    <item android:color="@color/grey_material2" android:state_checked="false"/>
</selector>

如您所見,我將芯片制作為可點擊且可檢查的(隱藏不需要的選中圖標)。

但是當我測試它時,沒有設置顏色。 芯片看起來只是默認顏色(灰度)

這種自定義樣式可以在哪里應用或如何應用?

PS:

我進行了快速測試,以查看我的CustomStyle是否格式錯誤/等。

我通過xml添加了一個視圖,並且運行良好。

<android.support.design.chip.Chip
                android:id="@+id/test"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/CustomChipChoice"
                android:checkable="true"
                android:clickable="true"
                app:checkedIconVisible="false"
                android:text="Chip Test"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM