在另一个布局中以编程方式扩展布局

时间:2014-01-28 08:29:53

标签: android xml android-layout android-inflate

我的Android应用程序需要帮助。我需要在另一个布局中膨胀布局,我不知道我是怎么做的。我的xml代码是这样的:

  • item.xml - 我需要膨胀多个xml(取决于变量号)

        <RelativeLayout
             android:id="@+id/cartel_N1"
             android:layout_width="150dp"
             android:layout_height="match_parent"
             android:background="@color/tabhost_background_pressed"
             android:layout_marginRight="22dp"
             android:orientation="vertical" >
    
        <ImageView 
            android:id="@+id/img_N1"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="15dp"
            android:layout_marginRight="15dp"
            android:src="@drawable/mcdonalds_icon" />
    
        <TextView 
            android:id="@+id/title_N1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="McDonals del CC Alcampo"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:textSize="15sp" />
    
        <TextView 
            android:id="@+id/categoria_N1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="CATEGORIA"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp" />
    
        <RelativeLayout 
            android:id="@+id/stars_and_distance_N1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <ImageView
                android:id="@+id/stars_N1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/stars_25"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="7dp" />
    
    
            <TextView
                android:id="@+id/distance_N1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="200m"
                android:textSize="12sp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="15dp"
                android:gravity="center_vertical"
                android:layout_marginTop="3dp" />
    
    
            <ImageView
                android:id="@+id/icon_pos_N1"
                android:layout_width="10dp"
                android:layout_height="10dp"
                android:src="@drawable/marker_distance"
                android:layout_toLeftOf="@id/distance_N1"
                android:layout_marginTop="7dp" />
    
        </RelativeLayout><LinearLayout
        android:id="@+id/cartel_N1"
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:background="@color/tabhost_background_pressed"
        android:layout_marginRight="22dp"
        android:orientation="vertical" >
    
        <ImageView 
            android:id="@+id/img_N1"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="15dp"
            android:layout_marginRight="15dp"
            android:src="@drawable/mcdonalds_icon" />
    
        <TextView 
            android:id="@+id/title_N1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="McDonals del CC Alcampo"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:textSize="15sp" />
    
        <TextView 
            android:id="@+id/categoria_N1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="CATEGORIA"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp" />
    
        <RelativeLayout 
            android:id="@+id/stars_and_distance_N1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <ImageView
                android:id="@+id/stars_N1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/stars_25"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="7dp" />
    
    
            <TextView
                android:id="@+id/distance_N1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="200m"
                android:textSize="12sp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="15dp"
                android:gravity="center_vertical"
                android:layout_marginTop="3dp" />
    
    
            <ImageView
                android:id="@+id/icon_pos_N1"
                android:layout_width="10dp"
                android:layout_height="10dp"
                android:src="@drawable/marker_distance"
                android:layout_toLeftOf="@id/distance_N1"
                android:layout_marginTop="7dp" />
    
        </RelativeLayout>
    
  • 这是我的主要xml:

    <ScrollView
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
    
      <LinearLayout
           android:id="@+id/container_destacado"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical" >
    
               <!-- Inflate multiple xml file here -->
    
          </LinearLayout>
    </ScrollView>
    

9 个答案:

答案 0 :(得分:107)

您可以使用类似

的内容
LayoutInflater inflater = LayoutInflater.from(context);

//to get the MainLayout
View view = inflater.inflate(container_destacado, null);
...
//Avoid pass null in the root it ignores spaces in the child layout

View inflatedLayout= inflater.inflate(R.layout.yourLayout, (ViewGroup) view, false);
containerDestacado.addView(inflatedLayout);

答案 1 :(得分:14)

您可以像下面这样实现:

LayoutInflater linf;
LinearLayout rr;

linf = (LayoutInflater) getApplicationContext().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);
linf = LayoutInflater.from(activity.this);

rr = (LinearLayout) findViewById(R.id.container_destacado);

for (int i = 1; i < NoOfTimes; i++) {

    final View v = linf.inflate(R.layout.item, null);
    rr.addView(v);
}

答案 2 :(得分:5)

在某些时候,您应该可以访问活动中的inflater,您可以使用以下代码调用它:

LayoutInflater li = LayoutInflater.from(context);

上下文是this或this.getActivity(),如果它是Fragment。然后用:

充气
View layout = li.inflate(R.layout.your_layout, null, false);

然后使用addView(layout)到container_destacado:

View containerDestacado = li.inflate(R.layout.container_destacado, null, false);
containerDestacado.addView(layout);

希望它有所帮助。

答案 3 :(得分:2)

ll = (LinearLayout) findViewById(R.id.container_destacado);  // ll is the layout where your inflated layout will be added 
linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int pos = 0;
    while (pos < noOfTimes) 
    {
        View myView = linflater.inflate(R.layout.item, null); //here item is the the layout you want to inflate 
        myView.setId(pos);
        /*
           You can change TextView text and ImageView images here e.g.
           TextView tv = (TextView)myView.findViewById(R.id.title_N1);
           tv.setText(pos);

        */
        pos++;
        ll.addView(myView);
    }

答案 4 :(得分:1)

Kotlin代码这样做:

 val layoutToInflate = 
 this.layoutInflater.inflate(R.layout.ly_custom_layout, 
  null)
container_destacado.addView(layoutToInflate )

答案 5 :(得分:1)

在Kotlin中,在item.xml上具有可编辑的textview元素

  val item = LayoutInflater.from(this).inflate(R.layout.item,null)
  val distance_N1_holder = item.findViewById<TextView>(R.id.distance_N1)
  distance_N1_holder.text = //dynamically generated value

  main.addView(item)

答案 6 :(得分:0)

您有想要给其他孩子充气的LinearLayout:

LinearLayout container = (LinearLayout)parentView.findViewById(R.id.container_destacado);

使用inflater加载item.xml后,您可以使用

container.addView(itemView);

答案 7 :(得分:0)

下面是我的一个项目的工作代码:

class MainPage extends Component {
  state = {
    data: [['name', 'max estimated diameter', 'min estimated diameter']]
  };

  componentDidMount() {
    axios
      .get('https://api.nasa.gov/neo/rest/v1/neo/browse?api_key=DEMO_KEY')

      .then(response => {
        this.setState({ data: response });
      })
      .catch(function(error) {
        console.log(error);
      });
  }
}

希望有帮助!

答案 8 :(得分:0)

如果有人无法获得 addView(),那是因为视图不是布局。