【问题标题】:How to set chipText on com.google.android.material.chip.Chip?如何在 com.google.android.material.chip.Chip 上设置chipText?
【发布时间】:2018-06-12 15:22:46
【问题描述】:

我正在调查提供的材料组件

implementation group: 'com.google.android.material', name: 'material', version: '1.0.0-alpha3'

com.google.android.material.chip.Chip

使用 Android Studio 版本

Android Studio 3.2 Canary 18
Build #AI-181.4892.42.32.4830125, built on June 8, 2018
JRE: 1.8.0_152-release-1136-b04 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Mac OS X 10.11.6

我用这个 gradle 创建了一个基本的应用程序

apply plugin: 'com.android.application'

android {
    compileSdkVersion 'android-P'
    defaultConfig {
        applicationId "com.research.peanut"
        minSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0-alpha3'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.1'

    implementation group: 'com.google.android.material', name: 'material', version: '1.0.0-alpha3'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
}

关注这个site

我在 layout.xml 中添加了以下芯片

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.google.android.material.chip.Chip
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:chipText="@string/hello_world" />

    <com.google.android.material.chip.Chip
        style="@style/Widget.MaterialComponents.Chip.Entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:chipIcon="@drawable/ic_add_circle_outline_24dp"
        app:chipText="@string/hello_world" />

    <com.google.android.material.chip.Chip
        style="@style/Widget.MaterialComponents.Chip.Filter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:chipText="@string/hello_world" />

    <com.google.android.material.chip.Chip
        style="@style/Widget.MaterialComponents.Chip.Choice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:chipText="@string/hello_world"/>


    <com.google.android.material.chip.Chip
        style="@style/Widget.MaterialComponents.Chip.Action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:chipIcon="@drawable/ic_call_to_action_24dp"
        app:chipText="@string/hello_world"/>

</LinearLayout>

我遇到以下构建问题

Android resource linking failed
Output:  /Users/frankb/AndroidStudioCanaryProjects/wip/Peanut/app/src/main/res/layout/activity_main.xml:10: error: attribute chipText (aka com.research.peanut:chipText) not found.
/Users/frankb/AndroidStudioCanaryProjects/wip/Peanut/app/src/main/res/layout/activity_main.xml:15: error: attribute chipText (aka com.research.peanut:chipText) not found.
/Users/frankb/AndroidStudioCanaryProjects/wip/Peanut/app/src/main/res/layout/activity_main.xml:22: error: attribute chipText (aka com.research.peanut:chipText) not found.
/Users/frankb/AndroidStudioCanaryProjects/wip/Peanut/app/src/main/res/layout/activity_main.xml:28: error: attribute chipText (aka com.research.peanut:chipText) not found.
/Users/frankb/AndroidStudioCanaryProjects/wip/Peanut/app/src/main/res/layout/activity_main.xml:35: error: attribute chipText (aka com.research.peanut:chipText) not found.
error: failed linking file resources.

我错过了什么?

更新

当我的 gradle 依赖项更改为

implementation group: 'com.google.android.material', name: 'material', version: '1.0.0-alpha1'

我的应用运行良好。

【问题讨论】:

  • 我在更新到1.0.0-alpha03 后遇到了同样的问题。以编程方式设置 chipText 顺便说一句。
  • @Nominalista,当我以编程方式设置芯片文本时,显示的芯片文本无法正确显示,因为芯片大小(宽度)设置为没有文本。你如何让文本完整显示?
  • 你调用的是setChiptText还是setText?第二个导致奇怪的行为。

标签: android android-chips


【解决方案1】:

版本1.0.0-beta01出来了,现在可以通过android:text设置文字了

【讨论】:

    【解决方案2】:

    仅仅因为 Chips extends Button 和 TextView 我们可以使用

    <android.support.design.chip.Chip
     .
     .
    android:text="text"/>
    

    【讨论】:

      猜你喜欢
      • 2018-12-07
      • 2019-01-26
      • 1970-01-01
      • 2014-10-18
      • 2013-10-28
      • 1970-01-01
      • 2015-08-22
      • 2013-04-14
      • 2018-11-07
      相关资源
      最近更新 更多