将视图/布局膨胀到另一个布局? [英] Inflate a view / layout into another layout?

查看:31
本文介绍了将视图/布局膨胀到另一个布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法将另一个布局膨胀到 android 中的第一个布局.怎么做呢?这是两个 XML 文件.第一个是主布局,第二个是我想扩展到第一个的布局.

I'm looking for a way to inflate another layout into the first layout in android. How would one do this? Here are the two XML files. The first is the main layout, the second is the layout I would like to inflate into the first.

我不能只包含布局,因为稍后我将使用此方法将其他布局扩展为线框.

I can't just include the layout as I will use this method to inflate other layouts into wire frames later on.

代码也在:http://pastebin.com/wjZ4s1cs,因为 stackoverflow 不喜欢 XML.>

Code also at: http://pastebin.com/wjZ4s1cs as stackoverflow does not like XML.

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff000000"
    >
    <TextView
        android:id="@+id/headerMenuText"
        android:text="@string/main_menu_title"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:textSize="16pt"
        android:paddingTop="10px"
        android:paddingBottom="10px"
        android:gravity="center"
    >
    </TextView>
    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_y="100dip"
        android:gravity="center"
        android:layout_gravity="center"
    >
        <TableRow
            android:gravity="center"
            android:layout_gravity="center">
            <ImageView
                android:id="@+id/menuItem1"
                android:layout_height="101dip"
                android:layout_width="89dip"
                android:src="@drawable/icon_settings"
            ></ImageView>

            <ImageView
                android:id="@+id/menuItem2"
                android:layout_height="101dip"
                android:layout_width="89dip"
                android:src="@drawable/icon_system_restart"
            ></ImageView>

            <ImageView
                android:id="@+id/menuItem3"
                android:layout_height="101dip"
                android:layout_width="89dip"
                android:src="@drawable/icon_game_history"
            ></ImageView>

            <ImageView
                android:id="@+id/menuItem4"
                android:layout_height="101dip"
                android:layout_width="89dip"
                android:src="@drawable/icon_game_correction"
            ></ImageView>

            <ImageView
                android:id="@+id/menuItem5"
                android:layout_height="101dip"
                android:layout_width="89dip"
                android:src="@drawable/icon_game_other"
            ></ImageView>
        </TableRow>

        <TableRow android:gravity="center">
            <TextView
                android:id="@+id/menuItemText1"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/main_menu_item_text_1"
                android:layout_gravity="center"
                android:gravity="center"
            ></TextView>

            <TextView
                android:id="@+id/menuItemText2"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/main_menu_item_text_2"
                android:layout_gravity="center"
                android:gravity="center"
            ></TextView>

            <TextView
                android:id="@+id/menuItemText3"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/main_menu_item_text_3"
                android:layout_gravity="center"
                android:gravity="center"
            ></TextView>

            <TextView
                android:id="@+id/menuItemText4"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/main_menu_item_text_4"
                android:layout_gravity="center"
                android:gravity="center"
            ></TextView>

            <TextView
                android:id="@+id/menuItemText5"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/main_menu_item_text_5"
                android:layout_gravity="center"
                android:gravity="center"
            ></TextView>

        </TableRow>
    </TableLayout>
    <View or layout
        android:id="@+id/screen_layout_bottom_menu"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
    </View or layout>
</AbsoluteLayout>

第二种布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/screen_bottom_menu"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    >
    <TableLayout
        android:id="@+id/screen_bottom_menu_table"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
    >
        <TableRow>
            <ImageView
                android:id="@+id/screen_bottom_menu_button_back"
                android:src="@drawable/back">
            </ImageView>

            <ImageView
                android:id="@+id/screen_bottom_menu_button_ok"
                android:src="@drawable/checkmark">
            </ImageView>

            <ImageView
                android:id="@+id/screen_bottom_menu_button_cancel"
                android:src="@drawable/xmark">
            </ImageView>

            <ImageView
                android:id="@+id/screen_bottom_menu_button_key_toggle"
                android:src="@drawable/lock">
            </ImageView>
        </TableRow>
    </TableLayout>
</LinearLayout>

推荐答案

ViewStub 但我从来没有用过它,我认为它不能使用超过一次.

There is ViewStub but I never used it and I think it can't be used more than once.

您可以扩充菜单布局并将其附加到主布局:

You can inflate the menu layout and attach it to the main layout:

AbsoluteLayout mainLayout = (AbsoluteLayout) findViewById(R.id.your_main_layout);
LayoutInflater inflater = 
              (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View menuLayout = inflater.inflate(R.layout.your_menu_layout, mainLayout, true);

然后当你想改变你可以删除它:

then when you want to change you can remove it:

mainLayout.removeView(menuLayout);

并以同样的方式添加另一个.

and add another the same way.

这会起作用,因为您想将布局添加为父布局的最后一个子项.如果你想添加它,比如说,在第一个位置,你可以在不将它附加到父级的情况下扩展你的布局(使用 false 作为最后一个参数),并手动添加它指定索引:

This will work because you want to add the layout as the last child of the parent layout. If you want to add it, say, at 1st position, you can inflate your layout without attaching it to the parent (use false as last arg), and adding it manually specifying the index:

mainLayout.addView(menuLayout, 0);

这篇关于将视图/布局膨胀到另一个布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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