簡體   English   中英

Android從單獨的xml膨脹

[英]Android inflate from separate xml

如何從單獨的xml文件中填充特定的View。 可能嗎? 當我嘗試設置此視圖的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

ingredients_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>          

嘗試這樣-

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

並且,使用View類對象,您可以識別布局中的其他組件。 例如,如果您的布局example有一個textView

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

基於此,只需像這樣標識您的FrameLayout

FrameLayout ingredients_frame = (FrameLayout)vi.findViewById(R.id.ingredients_frame);

用這個

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);
LayoutInflater inflater = LayoutInflater.from(this);
FrameLayout ingredients_frame = (FrameLayout)inflater.inflate(YOUR_LAYOUT, null);

inflate方法中的第一個參數是LAYOUT,但不是ID。 祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM