java - 是否可以使用复杂的 XML 模板文件动态生成 Android UI?

标签 java android xml android-layout templates

我试图通过创建一个 XML 模板来动态生成用户界面,该模板最终将附加到在 Activity XML 中静态定义的 LinearLayout 父级。静态定义的 LinearLayout 父级包装在 ScrollView 中。

在我的示例代码中,似乎我只能遍历模板的父 LinearLayout 而不能遍历其子元素。我也没有得到它的任何属性、按钮或图像。我使用 LayoutInflater 获取 XMl 并将其作为 LinearLayout 返回,因为它是我的模板文件的父级。任何想法或建议将不胜感激。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed);
        Context context = getApplicationContext();

        final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout b = (LinearLayout)inflater.inflate(R.layout.event, null);

        LinearLayout lLayout = (LinearLayout)findViewById(R.id.main_feed_parent);
        lLayout.addView(b);
    }

这是我试图动态添加到我的父 LinearLayout 的复杂 xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="325dp"
android:orientation="vertical"
android:weightSum="100">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="17"
    android:orientation="horizontal"
    android:weightSum="100">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="15"
        android:orientation="horizontal"
        android:weightSum="100">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="20"
            android:orientation="vertical"
            android:weightSum="100">

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="80"
            android:orientation="horizontal"
            android:weightSum="100">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentTop="true"
                android:layout_weight="50"
                android:paddingBottom="20dp"
                android:src="@drawable/thumbs1" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentTop="true"
                android:layout_weight="50"
                android:paddingTop="5dp"
                android:text="343"
                android:textSize="18dp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="12"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="100">
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_weight="33"
        android:orientation="horizontal"
        android:text="Join"
        android:textSize="11dp" />

   </LinearLayout>
</LinearLayout>

最佳答案

好的,我可以使用 fragment 加载模板 XML。在 onCreate() 期间添加了以下内容,修复了该问题...

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    EventActivity fragment = new EventActivity();
    fragmentTransaction.add(R.id.main_feed_parent, fragment);
    fragmentTransaction.commit();

关于java - 是否可以使用复杂的 XML 模板文件动态生成 Android UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32085102/

相关文章:

java - .length() 与 .getText().length() 与 .getText().toString().length()

java - 如何将 XML 文件输出到 Java 中的 REST Web 服务,以便另一个应用程序可以使用此 XML?

xml - 获取log4j2.xml中${catalina.base}的值

java - 代码错误 "org.openqa.selenium.remote.DriverCommand.NEW_SESSION"

java - 嵌入式 jetty 中的异步 servlet 似乎在高负载时写入错误响应

java - 在不使用任何第三方的情况下将 map 数据映射到同一卡片 View 中的 Recyclerview Adapter

java - 如何在 Recyclerview 中实现 onClicklistener 以打开不同的 Activity

php - 使用 PHP 和 URL 变量动态显示 XML

java - 如何避免两个节点之间的双重连接?

java - 使用 JAX-RS 和 DTO 参数进行 Bean 验证