我创建了一个以LinearLayout作为主要内容布局的抽屉布局:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<!-- The main content view -->
<ScrollView
android:id="@+id/scroll_wrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/content_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF" >
</LinearLayout>
</ScrollView>
<!-- The navigation drawer -->
<ListView
android:id="@+id/navigation"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/white"
android:dividerHeight="1dp"
android:background="#FFF"/>
</android.support.v4.widget.DrawerLayout>
现在在LinearLayout下没有视图可以在外部文件main_layout.xml
中创建布局,然后在上面的DrawerLayout中定义的此LinearLayout中调用该xml。
这样做的原因是我以编程方式构建UI,因此当用户从我的DrawerNavigation菜单中选择一个选项时,我调用content_container.removeAllViews();
然后我读取一个JSON模式并使用JSON中定义的Views填充content_container。但是,当用户再次返回主页时,我需要它来加载main_layout.xml
这样我就不必以编程方式构建家庭布局/视图。
答案 0 :(得分:0)
根据您的目标LayoutInflator,Fragments,Include
,这些链接可能会有所帮助答案 1 :(得分:0)
现在在LinearLayout下没有视图可以在外部文件main_layout.xml中创建一个布局,然后在我上面的DrawerLayout中定义的LinearLayout中调用该xml。
您的问题并非100%明确,但我假设您要在内部或外部存储中动态创建XML文件,然后将其作为布局进行充气。
如果是这种情况,那么答案是否定的,你不能这样做。即使有一种API方法允许我知道使用XML布局文件的路径,但它从未实现过并且无法工作。
布局文件在构建时进行预处理,以便提高布局效率并降低处理器/ GPU负载。在运行时创建布局的唯一方法是在内存中以编程方式执行。