如何在Android中将布局插入布局? [英] how to insert layout to layout in android?

查看:73
本文介绍了如何在Android中将布局插入布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个布局,第一个和第二个,我想插入第二个到第一个. 我想将第二个布局插入具有ID @ + id/layout的布局中 当我按下获取布局"按钮时,它会在按钮底部显示第二个布局

I've got two layouts first and second, I want to insert second to first. I want to insert second layout into layout that has id @+id/layout when I press button Get Layout, it show the the second layout on the bottom of button

第一个布局

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

    <Button
    android:id="@+id/btn_get_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Get Layout" />

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

</LinearLayout>

第二版面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/card_base"
android:orientation="vertical" >


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/img_cover"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/card_beauty" />

    <ImageView
        android:id="@+id/img_photo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="20dp"
        android:scaleType="fitXY"
        android:src="@drawable/sample_0" />
</RelativeLayout>

</LinearLayout>

推荐答案

    LinearLayout placeHolder = (LinearLayout) findViewByid(R.id.layout);
    getLayoutInflater().inflate(R.layout.second_layout, placeHolder);

这篇关于如何在Android中将布局插入布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆