1

我对 Android 开发非常陌生,我想动态地将一个页面添加到另一个页面。我是一名 C# Web 开发人员,并且想做与使用母版页并在此页面中插入其他页面相同的操作。

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

我目前正在处理 3 个主要文档:Pharma Manifest.xml MainActivity.java fragment_main_dummy.xml(我正在使用虚拟对象,因为它已经在做我想要的。)

这是 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>

我已经坐了好几天了。

我还是新手,无法弄清楚。我也有压力在大约一个月的时间内完成整个项目。任何帮助将不胜感激。

4

2 回答 2

0

您可以使用 LayoutInflater 来做到这一点。

例如。

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) );
于 2013-04-16T14:10:26.327 回答
0

你的标题在概念上是错误的,你没有一个 XML 到另一个。这些 XML 在编译期间经过大量处理和预编译,并且在最终应用程序中不存在。

此外,这些 XML 只是系统要构建的表示,Views并且在ViewGroups其中是extends View您可以调用的类.addView(view);

您使用的includexml 代码是重用静态生成的 XML 的好方法,但对于动态生成的东西,您需要通过代码来完成。

我注意到你正在使用碎片的东西。所以可能你最好使用动态添加/删除东西的片段路由

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

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

希望能帮助到你。

于 2013-04-16T14:22:08.290 回答