Android: раздувание определенного раздела XML-файла

#android

#Android

Вопрос:

Я хочу увеличить приведенный ниже код (взятый из list_item_icon_text.xml демонстрационных версий Android api).

 <?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

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

    <ImageView android:id="@ id/icon"
        android:layout_width="48dip"
        android:layout_height="48dip" />

    <TextView android:id="@ id/text"
        android:layout_gravity="center_vertical"
        android:layout_width="0dip"
        android:layout_weight="1.0"
        android:layout_height="wrap_content" />

</LinearLayout>
  

К сожалению, в демо-версии раздувание было выполнено с использованием

 convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
  

в list14.java .

Я хотел бы включить приведенный выше код в свой собственный XML-файл и только раздувать это LinearLayout, но я искал в Интернете и пытался сделать это часами. На данный момент я действительно разочарован. Мне удалось раздуть весь XML-файл, чего я не хотел. Я только хочу раздуть этот конкретный раздел (добавив его в свой XML-файл). Любая помощь приветствуется.

Ответ №1:

Вам просто нужно сохранить файл в своей layout папке, скажем ‘my_layout.xml `и при надувании вы будете использовать свое удостоверение личности :

      inflate(R.layout.my_layout);
  

Убедитесь, что выполнен правильный импорт. Я имею в виду импорт вашего R.java файла с помощью :

      import my.packaage.com.R;
  

Комментарии:

1. я не хочу раздувать весь свой макет. только код, приведенный выше, который я добавил в свой файл.

2. лучший вопрос: могу ли я раздуть LinearLayout, используя его android: id?