android - 将新的小部件控件动态添加到现有布局

标签 android android-layout widget

我已经看到这个问题被添加到堆栈中,但信息还没有帮助或成功,所以我仍然不太确定。

我想做的事情的要点:
我有一个在 xml 中定义的布局,例如 some_details_view.xml。

setContentView(R.layout.some_details_view);

它有一堆使用父相对布局、线性页眉布局、线性页脚布局、包含相对布局的中间滚动布局,其中包含一些标签-值类型的 TextView 。 在 ScrollView 相对布局的底部,我目前放置了一个框架布局作为占位符。

在创建相应的 Activity 时,我在各自的 TextView 中设置文本,其中包含从上一个 Activity 移交的数据;基本的东西。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white" >

    <LinearLayout
        android:id="@+id/header"

        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >

                        ...some header content

    </LinearLayout>         

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical" >

        ..some footer content

    </LinearLayout>

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/footer"
        android:layout_below="@+id/header"
        android:layout_margin="5dip" >


        <RelativeLayout
            android:layout_width="fill_parent"
            android:id="@+id/relativeScroll"
            android:layout_height="wrap_content" >

            ...text views in relative layout

            <FrameLayout
                android:id="@+id/placeholder"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_below="@+id/moreInfoValue" />

        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

为给定数据设置 TextView 后,我使用异步任务获取一些附加数据,我想将这些数据显示为静态表单布局底部的历史列表类型。可能有 0 个或多个项目,因此我要么添加 1 个或多个 TextView ,要么根本不添加。

在后执行中,据我所知是在主 UI 线程上运行,我尝试找到一个现有的容器布局/ View 组并添加一个新的线性布局,我向其中添加新的 TextView ,或者只添加新的 TextView 直接添加到现有容器布局。

这是我最近尝试过的:

ViewGroup mContainer = null; //defined as member variable of activity class and instatiated in on create
mContainer = (ViewGroup)findViewById(R.id.placeholder); //set in on create
LinearLayout ll = new LinearLayout(context); //on post execute of async task
ll.setOrientation(LinearLayout.VERTICAL);
mContainer.addView(ll); //add a new linear layout to an existing container layout
//add some new text view widgets items dynamically
for(NewDisplayItem item : items)
{
    TextView tv = new TextView(context);
    tv.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    tv.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    tv.setText(item.getSomeText());
    ll.addView(tv); //add text view to new linear layout
}

当 UI 加载时,在单步执行并观察使用上面的代码添加控件后,我没有看到新项目添加到我的布局中。

不确定它是什么,但除了它不起作用之外,这种方法似乎有些不对。当 Activity 加载时,所有静态 View 都已设置并在 View 中。我弹出一个加载对话框,逐步完成异步任务,我想我希望看到动态控件一个一个地添加到布局中?

最佳答案

首先textView.setWidth(int)以像素为单位的 width 作为参数。

其次,您还应该在要添加的 LinearLayout 上设置布局参数。

LayoutParams 的设置方式如下:

ll.setLayoutParams(new LinearLayout.LayoutParams(
       LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

同样适用于您的 TextViews

关于android - 将新的小部件控件动态添加到现有布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12216738/

相关文章:

java - 需要测试静态Utils类

android - 这个 android 菜单 View 的名称是什么?

python - tkinter 列表框用 python 拖放

c - Glade GTK 访问各个小部件

用于轮式选择器的 Android 阵列

java - Android - 检查 Runnable 是否正在运行

android - 字幕在小部件中经过一段时间后停止

android - 键盘出现时如何防止滑动布局

Android widget 不同的纵向和横向方向

java - 过滤器列表 : Begin by. .. Android Studio