0

如何从单独的 xml 文件中扩展特定视图。可能吗?当我尝试设置此视图的 id 时,我只有一个 ResourceNotFoundException。也许我需要以某种方式设置文件名?

LayoutInflater inflater = LayoutInflater.from(this);
FrameLayout ingredients_frame = (FrameLayout)inflater.inflate(R.id.ingredients_frame, null);

R.id.ingredients_frame 位于单独的 xml 文件中。

例外:

 android.content.res.Resources$NotFoundException: Resource ID #0x7f060032 type #0x12 is not valid

成分_frame_layout.xml:

<FrameLayout 
           xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ingredients_frame"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/expandable_second_layer_selector" >

            <TextView
                android:id="@+id/text"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:background="@android:color/white"
                android:singleLine="true"
                android:text="......................................................................................................................................................................"
                android:textColor="@color/dark_orange"
                android:textSize="18dip"/>

            <TextView
                android:id="@+id/ingredient_name"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="left"
                android:background="@android:color/white"
                android:textStyle="bold"
                android:text="@string/friends_total"
                android:textColor="@color/dark_orange"
                android:textSize="18dip" />

            <TextView
                android:id="@+id/ingredient_data"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="right"
                android:background="@android:color/white"
                android:textStyle="bold"
                android:textColor="@color/dark_orange"
                android:textSize="18dip" />
    </FrameLayout>          
4

3 回答 3

3

试试这样 -

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.ingredients_frame_layout, null);

而且,使用View类对象可以识别布局中的其他组件。例如,如果您的布局中有一个 textViewexample

TextView tv = (TextView)vi.findViewById(R.id.textView1);
tv.setText("Hello world");

基于此,只要确定你FrameLayout喜欢这个

FrameLayout ingredients_frame = (FrameLayout)vi.findViewById(R.id.ingredients_frame);
于 2012-07-23T10:04:27.277 回答
1
LayoutInflater inflater = LayoutInflater.from(this);
FrameLayout ingredients_frame = (FrameLayout)inflater.inflate(YOUR_LAYOUT, null);

inflate 方法的第一个参数是 LAYOUT,而不是 ID。祝你好运!

于 2012-07-23T10:09:13.287 回答
1

用这个

LayoutInflater mInflater = LayoutInflater.from(context);
View convertView = mInflater.inflate(R.layout.custom_list, null);

执行上述方式而不是采用框架布局,而不是像这样采用您的文本视图

TextView txtTitle = (TextView) convertView.findViewById(R.id.text);

TextView txtTitle2 = (TextView) convertView.findViewById(R.id.ingredient_name);

txtTitle.setText(yourtext);
于 2012-07-23T10:02:58.837 回答