检索 Layout/View 的属性列表

Retrieve List of Layout/View's attributes

我正在开发一个小型学习工具项目,该项目在各种片段中显示了多个不同的视图。我想做的是当用户选择布局或视图(例如 TextView)时,会弹出一个对话框,显示其所有可用属性(例如 android:layout_width、android:layout_height、android:focusable , ETC)。与其遍历每个 layout/view 并在文件或 class 中手动键入属性,我希望有一种方法可以通过编程方式获取属性。目前,我只有一个正在读取的 JSON 文件,但它变得非常乏味。不同的 API 版本也会出现问题。

为了了解我目前正在处理的事情,这是我的 JSON 文件:

{
  "LayoutParams": [
    "android:layout_height",
    "android:layout_width"
  ],

  "MarginLayoutParams": [
    "android:layout_margin",
    "android:layout_marginBottom",
    "android:layout_marginEnd",
    "android:layout_marginHorizontal",
    "android:layout_marginLeft",
    "android:layout_marginRight",
    "android:layout_marginStart",
    "android:layout_marginTop",
    "android:layout_marginVertical"
  ],

  "RelativeLayout": {
    "inherited_properties": [
      "MarginLayoutParams",
      "LayoutParams"
    ],
    "primary_properties": [{
        "android:gravity": ["bottom", "center", "center_horizontal", "center_vertical", "clip_horizontal", "clip_vertical", "end", "fill", "fill_horizontal", "fill_vertical", "left", "right", "start", "top"],
        "android:ignoreGravity": [""],
        "android:layout_above": [],
        "android:layout_alignBaseline": [],
        "android:layout_alignBottom ": [],
        "android:layout_alignEnd": [],
        "android:layout_alignLeft": [],
        "android:layout_alignParentBottom": [],
        "android:layout_alignParentEnd": [],
        "android:layout_alignParentLeft": [],
        "android:layout_alignParentRight": [],
        "android:layout_alignParentStart": [],
        "android:layout_alignParentTop": [],
        "android:layout_alignRight": [],
        "android:layout_alignStart": [],
        "android:layout_alignTop": [],
        "android:layout_alignWithParentIfMissing": [],
        "android:layout_below": [],
        "android:layout_centerHorizontal": [],
        "android:layout_centerInParent": [],
        "android:layout_centerVertical": [],
        "android:layout_toEndOf": [],
        "android:layout_toLeftOf": [],
        "android:layout_toRightOf": [],
        "android:layout_toStartOf": []
      }]
  }
}

您可以从 R.stylable 中提取它们。

AFAIK,没有定义 XML 架构或 DTD,因此 R.stylable 可能是您唯一的选择。