【问题标题】:Is it possible to dynamically generate Android UI using a complex XML template file?是否可以使用复杂的 XML 模板文件动态生成 Android UI?
【发布时间】:2015-08-19 01:27:18
【问题描述】:

我正在尝试通过创建一个 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>

【问题讨论】:

  • 我导入了您的代码,我可以很好地显示所有内容。你能分享R.layout.activity_feed的xml吗?
  • N.T.感谢您的帮助,我已经用更多信息更新了我的问题。我没有看到任何加载的图像或按钮。

标签: java android xml android-layout templates


【解决方案1】:

好的,所以我能够通过使用片段来加载模板 XML。在 onCreate() 期间添加了以下内容,修复了它...

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

【讨论】:

    【解决方案2】:

    它显示在我的模拟器上。话虽如此,它不会以您想要的方式显示。 您可以尝试以下几个更改:

    1. SearchView 和它旁边的 ImageView - 它们具有宽度的权重属性,但也设置了与父级匹配的宽度。将其更改为android:layout_width="0dp"
    2. 膨胀模板的方式会导致一些 xml 元素丢失。当您为了稍后将其添加到另一个视图而对视图进行膨胀时,您可能希望在其父“上下文”中进行膨胀:

    LinearLayout lLayout = (LinearLayout)findViewById(R.id.main_feed_parent); LinearLayout b = (LinearLayout)inflater.inflate(R.layout.event, lLayout, false);

    //this will return lLayout/parent, not the inflated view; template is automatically added to parent (LinearLayout)inflater.inflate(R.layout.event, lLayout, true);

    【讨论】:

    • 谢谢你的建议,我也试试,比较一下性能。
    猜你喜欢
    • 2013-10-30
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 2011-11-11
    • 2019-01-28
    相关资源
    最近更新 更多