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

标签 java android xml

我想包含一个动态内容以利用所有已创建的布局,而不必为每个 Activity 都提供包含所有功能的 xml。

我注意到 android 想要根据访问的 Activity 使用它来确定我的内容是什么。

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

}

最佳答案

您可以在您希望动态内容所在的 XML 中定义一个空布局,并通过代码添加它的内容。

像这样:

<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" />

然后,从您的代码中:

// 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);

关于java - 如何在 Android 中动态包含布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37790198/

相关文章:

java - 如何禁用 Netbeans 中的自动组件对齐?

java - 如何将 DateTimeFormatter 包含到项目中

javascript - 如何使用 Javascript 从 XML 键/值的 .text 获取完整值(包括任何附加标签)

c# - 如何将c#中的xml数据反序列化为java?

c# - 我将如何使用 System.Xml.Serialization 注释此 .net 类型以序列化/反序列化到/从以下 XML

java - 在 build.xml 中设置 ANT CLASSPATH

java - 适用于 iOS 和 Android 平台的跨平台 appium 代码

android - twitter4j 是否为传入的直接消息提供事件监听器?

java - JSON 数据格式

Android Studio 更新 Android 5.0 SDK : “A folder failed to be moved.”