Android 开发 - 在哪里可以找到 "original" 晦涩的组件文档

Android Development - Where to find "original" obscure component documentation

我的任务是创建一个 Android 应用程序,并在过去对 Android 进行了一些研究。我观看了 developer.android.com 的几个视频教程,并且对 Android 中的事物如何工作以及文件系统如何布局和协同工作有了一定的了解。

最近我一直在探索在各种组件上制作自己的样式,现在我正在尝试为 ProgressBar 设计样式。我了解到 progressDrawable 属性可以指向一个图层列表,其中包含设置背景样式、进度和次级进度的项目。

我不明白的是,人们如何确切地知道图层列表中要包含哪些 ID,以及这些 ID 究竟引用了什么?我通过文档、文章、API 演示、源代码(在 Android Studio 中按 Ctrl+单击)等在线搜索了几个小时。但我找不到一个地方提到这些信息最初来自哪里。人们如何知道层列表中包含哪三个 ID?如果有人能够 link 确切地知道官方信息的位置以及这些来自 ids.xml 的 id 在样式组件中的使用位置和方式,那就太好了。

谢谢!

how do people know exactly which IDs to include in the layer list, and what exactly those IDs reference?

如果它不在 ProgressBar 文档中,我会找到一个 framework-supplied ProgressBar 可绘制对象并查看它们做了什么。例如,查看 $ANDROID_SDK/platforms/android-$API/data/res/drawable/progress_horizontal_material.xml,其中:

  • $ANDROID_SDK 是安装 Android SDK 的地方
  • $API 是一个 API 级别,例如 29(注意:因为我指的是 progress_horizontal_material.xml,所以 API 级别需要是 21 或更高)

如果您在那里查看,您会看到 ProgressBar 的股票 Theme.Material 背景是:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@id/background"
          android:gravity="center_vertical|fill_horizontal">
        <shape android:shape="rectangle"
               android:tint="?attr/colorProgressBackgroundNormal">
            <corners android:radius="?attr/progressBarCornerRadius" />
            <size android:height="@dimen/progress_bar_height_material" />
            <solid android:color="@color/white_disabled_material" />
        </shape>
    </item>
    <item android:id="@id/secondaryProgress"
          android:gravity="center_vertical|fill_horizontal">
        <scale android:scaleWidth="100%">
            <shape android:shape="rectangle"
                   android:tint="?attr/colorControlActivated">
                <corners android:radius="?attr/progressBarCornerRadius" />
                <size android:height="@dimen/progress_bar_height_material" />
                <solid android:color="@color/white_disabled_material" />
            </shape>
        </scale>
    </item>
    <item android:id="@id/progress"
          android:gravity="center_vertical|fill_horizontal">
        <scale android:scaleWidth="100%">
            <shape android:shape="rectangle"
                   android:tint="?attr/colorControlActivated">
                <corners android:radius="?attr/progressBarCornerRadius" />
                <size android:height="@dimen/progress_bar_height_material" />
                <solid android:color="@color/white" />
            </shape>
        </scale>
    </item>
</layer-list>

(基于 API 级别 29;其他版本可能有所不同,但可能差别不大)

您可以将该文件克隆到项目的 res/drawable/ 目录中,根据需要对其进行调整,然后使用调整后的版本。

IOW,如果文档中没有,"official" 信息就是 Android 源代码。

请注意,对于 Theme.Material,如果您需要做的只是更改颜色,则可以不理会 drawable,并在 ProgressBar 上使用各种色调属性。