【问题标题】:Custom Layout not detecting in mainActivity class?在 mainActivity 类中未检测到自定义布局?
【发布时间】:2015-02-24 06:47:57
【问题描述】:

我是安卓初学者。

我正在使用Android Studio 1.0。我正在尝试为listView 制作custom layout。我在res/layout named row_layout 中创建了自定义布局文件。布局的xml是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/cl_textView"
    android:textSize="30sp"
    android:textStyle="bold"
    android:padding="15dp"/>
</LinearLayout>

在 MainActivity 类中实现该自定义布局的代码是:

  ListAdapter theAdapter = new ArrayAdapter<String>(this, android.R.layout.row_layout,toDoList);

但是活动类没有检测到这个自定义布局,并在构建时显示以下错误。

【问题讨论】:

  • 删除android.R.layout.row_layout并使用R.layout.row_layout
  • 啊!哈!你解决了兄弟,非常感谢你的及时回复。我正在关注一个教程,那里是 android.R.layout.row_layout 。有什么区别,请您详细说明。
  • android sdk 有自己的 listview 布局。要使用它们,您需要调用android.R,并且您自己的自定义视图已在Gen[Generated Java Files]R.java 文件中注册,使用它们您只需调用R.layout.views
  • 非常感谢消除了我的困惑。
  • @PrajeetShrestha,如果您的问题得到解决,请将明确答案之一标记为正确。

标签: java android android-layout android-studio


【解决方案1】:

您必须将android.R. 替换为R.,因为R.* 提供您的应用程序资源,android.R.* 提供Android SDK 附带的资源。 (R.* 实际上是your.package.R.* 的快捷方式)

希望对你有帮助。

【讨论】:

    【解决方案2】:

    R.layout 表示您的应用程序提供的资源。您的应用程序定义的所有变量、资源文件(可绘制、字符串、布局等)都可以通过R 访问。

    例如R.layout.*, r.drawable.*, R.id.*, R.color.* etc

    但是android.R 表示你的android SDK 的资源。如果您使用android.R,所有不是您定义但由android SDK定义的资源都将可供您使用

    在您的代码中,您使用的是自己的布局,因此请将 android.R.layout 替换为 R.layout

    【讨论】:

    • +100简要说明R.layoutandroid.R.layout的区别
    【解决方案3】:

    应该是:

    ListAdapter theAdapter = new ArrayAdapter<String>(this, R.layout.row_layout,toDoList);
    

    android.R指的是android sdk中预建的那些资源。

    由于您已经为自定义列表视图定义了自己的布局,因此您应该使用项目中生成的 R 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2016-08-14
      • 2023-03-20
      相关资源
      最近更新 更多