Android layout 动态图像视图的标识符
我正在构建一个应用程序,我必须在其中更改布局中ImageView元素的源Android layout 动态图像视图的标识符,android-layout,android-fragments,android-imageview,Android Layout,Android Fragments,Android Imageview,我正在构建一个应用程序,我必须在其中更改布局中ImageView元素的源 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="24sp"/>
<ImageView
android:id="@android:id/image1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/res0"
android:contentDescription="@string/description" />
<TextView
android:id="@android:id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
我在ImageView上尝试了同样的方法,设置android:id=@android:id/image1,但是Eclipse一直说没有找到与给定名称匹配的资源。我做错了什么?使用android.R.id.text1您不是在创建id,而是在使用内置id,但是没有内置的android.R.id.image1
您需要为ImageView创建自己的ID,因此在XML中而不是
android:id="@android:id/image1"
ImageView image = (ImageView) rootView.findViewById(android.R.id.image1);
做
这告诉编译器在编译时创建一个名为image1的新id。以这种方式创建的id无法通过android访问。R.id。???由于这仅用于内置ID,因此在活动中而不是
android:id="@android:id/image1"
ImageView image = (ImageView) rootView.findViewById(android.R.id.image1);
做
作品这些保留id的实际用途是什么?@Ciambello它们之所以存在,主要是因为您需要使用内置id来使用某些元素的功能,例如,要使用ListActivity,您的布局必须具有id=@android:id/list的ListView
ImageView image = (ImageView) rootView.findViewById(R.id.image1);