2013-04-16 13 views
1

我对Android开发非常陌生,并且想要动态地将页面添加到另一个页面。我是一名C#Web开发人员,想要使用母版页并在此页面中插入其他页面。在Android中使用Eclipse动态地在另一个XML文档中添加XML文件内容

我此刻的代码如下:(记住,我从来没有这样做,任何和所有的提醒,将不胜感激。)

我有我的工作3个主要文件就在此刻: 制药的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; 
     } 
    } 
} 
内容

这里是清单中的代码:为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> 

<?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> 

而且最后的代码中,我坐在这个好几天。

我对此仍然陌生,无法弄清楚。我也被迫在大约一个月的时间内完成整个项目。任何帮助将不胜感激。

回答

0

您的标题在概念上是错误的,您不会将XML转换为另一个XML。这些XML在编译期间会被大量捣毁和预编译,并且不会像您在最终应用中看到的那样存在。

此外,这些XML只是为系统建立Views和内部ViewGroups表示这是extends View你可以调用.addView(view);

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

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

你在onNavigationItemSelected内创建的代码几乎是你需要做的动态改变东西的一切。

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

希望它有帮助。

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)); 
相关问题