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

java– 如何在Android中动态包含布局?,第1张

概述我想包含一个动态内容来利用已经创建的所有布局,而不必拥有包含每个actitivy所有功能的xml.根据所访问的活动,我注意到android有一个想用它来确定我的内容是什么.<?xmlversion="1.0"encoding="utf-8"?><android.support.design.widget.CoordinatorLayoutxmlns:android="http

我想包含一个动态内容来利用已经创建的所有布局,而不必拥有包含每个actitivy所有功能的xml.

根据所访问的活动,我注意到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    }}

解决方法:

您可以在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 layoutlinearLayout dynamicContent = (linearLayout) findVIEwByID(R.ID.dynamic_content);// assuming your Wizard content is in content_wizard.xmlVIEw wizardVIEw = getLayoutInflater()    .inflate(R.layout.content_wizard, dynamicContent, false);// add the inflated VIEw to the layoutdynamicContent.addVIEw(wizardVIEw);
总结

以上是内存溢出为你收集整理的java – 如何在Android中动态包含布局?全部内容,希望文章能够帮你解决java – 如何在Android中动态包含布局?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1115133.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存