简体   繁体   English

如何在Android中动态包含布局?

[英]How to dynamically include a layout in Android?

I wanted to include a dynamic content to take advantage of all layout already created without having to have an xml with all the features for each actitivy. 我想包含一个动态内容来利用已经创建的所有布局,而不必拥有包含每个actitivy所有功能的xml。

I noticed that the android has an wanted to use this to determine what will be my content, according to the activity accessed. 根据所访问的活动,我注意到android有一个想用它来确定我的内容是什么。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="br.com.p21sistemas.certidao21.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include android:id="@+id/content" layout="@layout/content_??????" /> 

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

public class WizardActivity extends AppCompatActivity {

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

        // Something I do not know what may include my content content_wizard
    }

}

You could define an empty layout in your XML where you would like you dynamic content to be and add it's content from code. 您可以在XML中定义一个空的布局,您希望在其中添加动态内容,并从代码中添加内容。

Something like this: 像这样的东西:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>

<LinearLayout
    android:id="@+id/dynamic_content"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

And then, from your code: 然后,从您的代码:

// get ahold of the instance of your layout
LinearLayout dynamicContent = (LinearLayout) findViewById(R.id.dynamic_content);

// assuming your Wizard content is in content_wizard.xml
View wizardView = getLayoutInflater()
    .inflate(R.layout.content_wizard, dynamicContent, false);

// add the inflated View to the layout
dynamicContent.addView(wizardView);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM