bfa*_*ahm 4 java android android-studio material-components-android
我想在我的应用程序中实现这种外观:

我在活动中的 xml 代码看起来像这样:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.google.android.material.button.MaterialButton
android:id="@+id/button1"
... />
<com.google.android.material.button.MaterialButton
android:id="@+id/button2"
... />
<com.google.android.material.button.MaterialButton
android:id="@+id/button3"
... />
</com.google.android.material.button.MaterialButtonToggleGroup>
Run Code Online (Sandbox Code Playgroud)
我迁移到 androidx,并在我的样式中使用了材质组件,但由于某种原因,我收到一个运行时错误,内容为:
Binary XML file: Error inflating class com.google.android.material.button.MaterialButtonToggleGroup
implementation "com.google.android.material:material:1.0.0"
Run Code Online (Sandbox Code Playgroud)
不过,在com.google.android.material.button.MaterialButton没有组的情况下使用时,它会工作得很好,这意味着我使用了正确的样式和 gradle 实现。
我还尝试以下操作:
androidx.appcompat:appcompat:1.1.0和com.google.android.material:material:1.0.0多次这是我的依赖项(以防其中一些导致某种冲突):
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
//Room Persistance Library
def room_version = "2.1.0"
implementation "android.arch.persistence.room:runtime:$room_version"
annotationProcessor "android.arch.persistence.room:compiler:$room_version"
//Lifecycle Libraries from the Android Architecture Components
def lifecyle_version = " 2.1.0"
implementation "android.arch.lifecycle:extensions:$lifecyle_version"
annotationProcessor "android.arch.lifecycle:compiler:$room_version"
//External Libraries:
implementation 'com.github.HITGIF:TextFieldBoxes:1.4.4'
implementation "com.google.android.material:material:1.0.0"
Run Code Online (Sandbox Code Playgroud)
这是我用于该活动的样式:
<style name="ThemeNewRoutine" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
这是清单文件中的活动元素:
<activity
android:name=".Views.NewRoutine.NewRoutineActivity"
android:theme="@style/ThemeNewRoutine">
</activity>
Run Code Online (Sandbox Code Playgroud)
为什么 com.google.android.material.button.MaterialButton 工作得很好,但 com.google.android.material.button.MaterialButtonToggleGroup 不是。
此外,我发现在 style.xml 文件中使用以下属性时也无法解析它们(即使父级是 Theme.MaterialComponents.Light):
我不知道这是否会导致任何冲突,但我的活动扩展了 AppCompatActivity:
public class NewRoutineActivity extends AppCompatActivity
Run Code Online (Sandbox Code Playgroud)
我在我的应用程序中遇到了同样的问题,最终可以通过将材料库更新到最新版本“1.2.0-alpha02”来解决它。
不幸的是,我不知道为什么它不适用于 v1.0.0,因为有些示例适用于这个版本。
implementation 'com.google.android.material:material:1.2.0-alpha02'
| 归档时间: |
|
| 查看次数: |
3157 次 |
| 最近记录: |