Android 为 linearLayout 设置 id 参数



我正在尝试以编程方式将一些视图添加到已在xml文件中定义的线性布局中。

这是我定义线性布局的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

但是,如您所见,它没有id元素。如何添加一个,以便我可以从我的活动中调用方法 findViewById 来检索布局?我尝试手动添加它,但它没有在 R.java 中生成。

谢谢

嗯,像这样?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linearlayout1"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 

如果添加它并遇到问题,请尝试 Project -> 清理。

如果所有其他方法都失败了,请在 gen -> 软件包目录中找到并核弹 R.java 文件以强制重建它。

最新更新