【问题标题】:Where can I find simple_spinner_item in AndroidX?我在哪里可以找到 AndroidX 中的 simple_spinner_item?
【发布时间】:2019-09-05 00:30:35
【问题描述】:

我在我的应用程序中使用了一个初始化如下的 ArrayAdapter:

myAdapter = new ArrayAdapter(context, 
    android.R.layout.simple_spinner_item,
    myList
);

AndroidX包中android.R.layout.simple_spinner_item的对应地址在哪里?

【问题讨论】:

  • 真的没有。这只是一个<TextView>。但是,AppCompatActivity 使用的 LayoutInflater 将自动用 AppCompatTextView 替换 TextView
  • 也就是说,如果您愿意,可以继续使用android.R.layout.simple_spinner_item。如果你想自定义它,你可以创建自己的布局。
  • 感谢您的评论@MikeM。我现在明白了。

标签: android android-spinner androidx


【解决方案1】:

所有布局都位于:sdk\platforms\android-xx\data\res\layout 查看布局的XML:

Eclipse:只需在代码中的某处输入android.R.layout.simple_spinner_item,按住Ctrl,将鼠标悬停在simple_spinner_item 上,然后从出现的下拉列表中选择“在layout/simple_spinner_item.xml 中打开声明”。它会将您定向到 XML 的内容。

Android Studio: Project Window -> External Libraries -> Android X Platform -> res -> layout,在这里您将看到可用布局的列表。

【讨论】:

    【解决方案2】:

    (在 Android Studio 中)如果您右键单击它并选择转到声明,您会发现以下内容:

     <?xml version="1.0" encoding="utf-8"?>
        <!--
        /* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
        **
        ** Copyright 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.
        */
        -->
        <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
            android:id="@android:id/text1"
            style="?android:attr/spinnerItemStyle"
            android:singleLine="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:textAlignment="inherit"/>
    

    在 cmets 之后编辑:

    simple_spinner_item was added on API 1 所以它被所有版本的 Android 支持。 AndroidX 是支持库的延续,支持库中添加了更新的 API 并需要向后兼容的功能。在这种情况下,它不会影响此特定布局的使用,因为它在所有版本中都可用。您可以继续使用它。我将保留上面的布局内容,以防您想要覆盖样式并按照上面的 cmets 中提到的那样自定义它。

    【讨论】:

    • 感谢您回答我,但能否请您也提供代码?
    • @Jorn 也许我误解了你的问题。您在寻找文件的位置吗?你在找什么代码?
    • 我正在尝试像android.R.layout.simple_spinner_item这样找到对应的资源id,因此可以将其传递给ArrayAdapter构造函数。
    • @Jorn 对应什么?为什么不能按原样使用 android.R.layout.simple_spinner_item?
    • 我认为我需要使用AndroidX中存在的相应simple_spinner_item。可以继续使用吗?
    猜你喜欢
    • 1970-01-01
    • 2014-07-16
    • 2012-05-30
    • 2013-07-13
    • 2012-06-05
    • 2011-04-24
    • 2021-02-02
    • 2016-07-02
    • 1970-01-01
    相关资源
    最近更新 更多