Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中使用Eclipse在另一个XML文档中动态添加XML文件内容_Android_Android Layout - Fatal编程技术网

在Android中使用Eclipse在另一个XML文档中动态添加XML文件内容

在Android中使用Eclipse在另一个XML文档中动态添加XML文件内容,android,android-layout,Android,Android Layout,我是Android开发的新手,我想在另一个页面上动态添加一个页面。我是一名C#web开发人员,我希望使用母版页并在此页面中插入其他页面 我目前的代码如下:(请记住,我从来没有这样做过,任何建议都将不胜感激。) 我目前正在处理3个主要文件: Pharma Manifest.xml MainActivity.java fragment_main_dummy.xml(我使用的是dummy,因为它已经在做我想要的事情了。) 以下是MainActivity.xml上的内容 package com.phar

我是Android开发的新手,我想在另一个页面上动态添加一个页面。我是一名C#web开发人员,我希望使用母版页并在此页面中插入其他页面

我目前的代码如下:(请记住,我从来没有这样做过,任何建议都将不胜感激。)

我目前正在处理3个主要文件: Pharma Manifest.xml MainActivity.java fragment_main_dummy.xml(我使用的是dummy,因为它已经在做我想要的事情了。)

以下是MainActivity.xml上的内容

package com.pharma.pharma;

import org.w3c.dom.Text;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.location.Address;
import android.os.Bundle;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements
        ActionBar.OnNavigationListener {

    /**
     * The serialization (saved instance state) Bundle key representing the
     * current dropdown position.
     */
    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the action bar to show a dropdown list.
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        // Set up the dropdown list navigation in the action bar.
        actionBar.setListNavigationCallbacks(
        // Specify a SpinnerAdapter to populate the dropdown list.
                new ArrayAdapter<String>(getActionBarThemedContextCompat(),
                        android.R.layout.simple_list_item_1,
                        android.R.id.text1, new String[] {
                                getString(R.string.title_Dashboard),
                                getString(R.string.title_Customers),
                                getString(R.string.title_Products),
                                getString(R.string.title_Detailing),
                                getString(R.string.title_Appointments),
                                getString(R.string.title_Events), }), this);
    }

    /**
     * Backward-compatible version of {@link ActionBar#getThemedContext()} that
     * simply returns the {@link android.app.Activity} if
     * <code>getThemedContext</code> is unavailable.
     */
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private Context getActionBarThemedContextCompat() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return getActionBar().getThemedContext();
        } else {
            return this;
        }
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Restore the previously serialized current dropdown position.
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getActionBar().setSelectedNavigationItem(
                    savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // Serialize the current dropdown position.
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
                .getSelectedNavigationIndex());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onNavigationItemSelected(int position, long id) {
        // When the given dropdown item is selected, show its contents in the
        // container view.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, fragment).commit();
        return true;
    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);

            TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
            dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));

            switch(getArguments().getInt(ARG_SECTION_NUMBER)){
            case 1:
                dummyTextView.setText("blah Blah Dashboard");
                break;
            case 2:
                dummyTextView.setText("blah Blah Customers");
                break;
            case 3:
                dummyTextView.setText("blah Blah Products");
                break;
            case 4:
                dummyTextView.setText("blah Blah Detailing");
                break;
            case 5:
                dummyTextView.setText("blah Blah Appointments");
                break;
            case 6:
                //Insert XML to fragment dummy here as a test
                break;
                default:
                    //throw error
            }
            return rootView;
        }
    }
}
以下是清单中的代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pharma.pharma"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.pharma.pharma.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

最后是fragment_main_dummy.xml的代码

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$DummySectionFragment" >

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textIsSelectable="true" />

    <include
        android:id="@+id/section_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        layout="@layout/dashboard" />

</RelativeLayout>

我已经坐了好几天了


我对这一点还不熟悉,还没弄明白。我也有压力在大约一个月内完成整个项目。任何帮助都将不胜感激。

您可以使用LayoutInflater来完成

例如


您的标题在概念上是错误的,您不能从一个XML转换到另一个XML。这些XML在编译过程中被大量压缩和预编译,在最终的应用程序中并不存在

此外,这些XML只是系统构建
视图
视图组
内部的表示,这是一个
扩展视图
的类,您可以调用
.addView(View)

您使用的
include
xml代码是重用静态生成的xml的好方法,但对于动态生成的东西,您需要通过代码来实现

我注意到你用的是碎片。因此,您最好使用动态添加/删除内容的片段路径

您在
onNavigationItemSelected
中创建的代码几乎就是动态更改内容所需的一切

您正在创建/实例化的片段将覆盖
onCreateView
,以膨胀新视图并返回它。该新视图将插入到android.R.id.content
(即整个内容的视图id)或XML中指定的任何id上

希望能有帮助

RelativeLayout relativeLayout= (RelativeLayout) findViewById(R.id.relativeLayout);

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

relativeLayout.addView(childIndex, layoutInflater.inflate(R.layout.newLayoutToAdd, this, false) );