【问题标题】:Using android:id="android:id/list" [duplicate]使用 android:id="android:id/list" [重复]
【发布时间】:2015-07-29 13:14:11
【问题描述】:

我正在 GitHub 上使用这个 AlhabetScroller https://github.com/brightec/AlphabetScroller/blob/master/src/uk/co/brightec/alphabetscroller/AlphabetListAdapter.java
有文件list_alphabet.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:fastScrollEnabled="true" />

    <LinearLayout
        android:id="@+id/sideIndex"
        android:layout_width="40dip"
        android:layout_height="fill_parent"
        android:background="#FFF"
        android:gravity="center_horizontal"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

我的问题与这一行有关:
android:id="@android:id/list"

通常,我会这样做:
android:id="@+id/my_list"

就像他对 sideIndex 所做的那样。相反,他使用的东西似乎使用了 Android 代码中已经存在的东西。
这样做的目的是什么?为什么这样做而不是我通常的做法?

编辑:
感谢拉利特·波塔尼。这个问题确实是重复的。

【问题讨论】:

  • 如果您要扩展 ListActivityListFragment,那么您必须使用android:id="@android:id/list"。否则,您可以随意使用任何您喜欢的名称(甚至在同一个 Activity 或 Fragment 中使用多个 ListView!)。这是一个非常古老的代码:我看到他们仍然使用fill_parent(在 API 级别 8 上已弃用)。而且.. 是的,我看到他们正在扩展 ListActivity:public class MainActivity extends ListActivity {
  • 感谢您的回复。我也注意到了fill_parent。你知道这个功能的当代例子吗?
  • 从 API 级别 8 开始,使用match_parent
  • ...类似this?
  • 谢谢,我去看看。

标签: android android-layout


【解决方案1】:

如果您使用 ListFragment 或 ListActivity 您不必提供视图,并且 Android 已经有一个预定义的视图和一个 id 来查找它,它会自动执行。在这里,开发人员添加了一些东西,但为了仍然使用 ListFragment 或 ListActivity 功能,他使用了相同的 id。

More info in ListFragment.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-23
    • 2015-10-18
    • 2013-03-07
    • 2015-05-17
    • 1970-01-01
    • 2014-02-09
    • 2022-08-23
    • 2011-01-04
    相关资源
    最近更新 更多