gpt4 book ai didi

android - 在哪里以及如何找到 Android 类使用的布局文件?

转载 作者:行者123 更新时间:2023-12-02 18:39:53 26 4
gpt4 key购买 nike

问题

Android SDK 类引用使用资源 ID 的预定义布局。

在我的示例中,我正在创建一个自定义 Preference类并希望为该首选项类型创建自定义布局(包括自定义小部件)。这一切都很好,例如我可以有一个 TextView显示 android:title 指定的首选项标题在 XML 中(没有自定义属性)。

问题是我希望自定义布局的样式符合标准Android Preference 使用的样式类。例如,这包括文本大小和样式。我可以简单地尝试一下以找到匹配的样式,但我怀疑 Android 使用的样式是通用的,因此它会适应应用程序的通用样式。

方法

我查看了Preference SDK 中的类,发现对于标题它执行以下操作:

TextView titleView = (TextView) view.findViewById(com.android.internal.R.id.title)

这暗示所有样式都在布局文件中定义。现在我的问题是:如何找到这个布局文件?我想知道这里使用的资源id是否足够,它看起来很通用,甚至可能用于多种用途(即Preference类之外的其他地方的标题)。是这样吗?

为了找到布局文件,我在 <android-sdk>/platforms/android-24/data/res/layout 中查找了示例。它确实包含一些名称中包含“首选项”的资源。

例如,文件 preference_category.xml看起来如下:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/listSeparatorTextViewStyle"
android:id="@+android:id/title"
/>

这里是title id 已定义。上面那个id是com.android.internal.R.id.title吗?会引用吗?

我可以找到更多相关的布局文件,其中 preference_child.xml看起来很有前途:

<TextView android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />

但我仍然必须猜测这是否是我正在寻找的文件。因此问题是:如何找到正确的布局文件? XML 文件中没有太多文档。 一般如何找到Android框架中不同位置使用的布局文件?

相关问题( Where can I find default AlertDialog layout xml file? )的答案表明我查看了 SDK 中的正确目录。然而,答案并没有说明一般情况下如何找到所需的布局。

自定义的具体案例Preference

相关帖子:Android: Creating custom preference

在链接的答案中,作者建议(通过给定的代码)创建一个自己的标题 TextView 。我想知道是否可以重用 Android 提供的布局,保留 TextView s 作为标题和摘要,只需在 XML 中注明的位置添加自己的小部件,id 为 @android:id/widget_frame 。如果不复制它并在新的布局文件中进行调整,这是否可能?完整文件preference_child.xml从Android SDK看起来如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 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.
-->

<!-- Layout for a visually child-like Preference in a PreferenceActivity. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingStart="16dip"
android:paddingEnd="?android:attr/scrollbarSize">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="@dimen/preference_widget_width"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">

<TextView android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />

<TextView android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:layout_alignStart="@android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:maxLines="4" />

</RelativeLayout>

<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="@+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="@dimen/preference_widget_width"
android:gravity="center"
android:orientation="vertical" />

</LinearLayout>

最佳答案

您可以通过编程方式完成此操作。首先需要获取Activity的rootView,看这里https://stackoverflow.com/a/4488149/2895571 。然后您可以尝试在该 rootView 上findViewById()

关于android - 在哪里以及如何找到 Android 类使用的布局文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40196015/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com