动态添加视图到布局

3

作为一个相对新手,在Android平台上我遇到了很多困难,像我一样的人也很多。我不知道到目前为止我已经浪费了多少时间 - 即使计算它们也是可怕的。

基本上,我想在点击按钮后将新的TextView(或任何其他View)添加到LinearLayout中。以下是代码的这部分:

public void btnClick(View view) {

    final LinearLayout ll = (LinearLayout) findViewById(R.id.test_story_screen_layout);

    //checking if child is being added - it is, this value is increase with every button click
    android.util.Log.d("child: ", Integer.toString(ll.getChildCount()));
    android.util.Log.d("height: ", Integer.toString(ll.getMeasuredHeight()));

    ll.post(new Runnable() {

        public void run() {
            final TextView tv = new TextView(MainActivity.this);
            tv.setText("new one " + (new Random()).nextInt());
            tv.setTextColor(Color.YELLOW);
            LayoutParams lp = new LayoutParams(200, 500);
            tv.setLayoutParams(lp);
            tv.setVisibility(View.VISIBLE);
            ll.addView(tv, ll.getChildCount());

            //checking if the run() is called - it is
            ll.setBackgroundColor(Color.GREEN);
        }
    });

    ll.requestLayout(); //invalidate() and postInvalidate() also not working
}

但是新增的View不可见(但已作为LinearLayout的子项添加)。

经过数小时的检查,我只发现当我替换了这行代码时:

            ll.addView(tv, ll.getChildCount());

使用以下内容:

            ll.addView(tv, ll.getChildCount() - 1);

新视图已经可见,但它替换了先前的视图 - 这是不期望的。
我已经检查过一些解决方案,例如这个:刷新LinearLayout后添加视图 它们没有帮助我解决问题。
更新:
在XML中预定义两个视图时,线性布局看起来很好(如下面的代码所示)。以下是XML的有趣部分:
<LinearLayout
    android:id="@+id/test_story_screen_layout"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:layout_margin="16dp" >
    <ImageView
        android:id="@+id/imageView1"
        android:src="@drawable/example"
        android:layout_width="16dp"
        android:layout_height="16dp" />
    <TextView
        android:id="@+id/textView1"
        android:text="Hello World!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

我觉得没有什么不寻常的。因此,这两个视图(图像和文本)当然是可见的。然后,通过上面代码中的btnClick()方法动态地将任何新的TextView添加到此LinearLayout中时,它是不可见的,但实际上已添加到布局中,因为每次添加子视图(=按钮点击)时,ll.getChildCount()都会增加。

为了进一步测试这个问题,在btnClick()方法的末尾添加了这两行代码:

    android.util.Log.d("tv.getMeasuredWidth: ", Integer.toString(tv.getMeasuredWidth()));
    android.util.Log.d("tv.getMeasuredHeight: ", Integer.toString(tv.getMeasuredHeight()));

我猜问题出在电视(TextView)上而不是线性布局(LinearLayout),因为电视的宽度和高度都为0。

是的,ll.post()在这里没有任何意义,因为您是在onClick事件监听器中添加视图,这意味着视图已经渲染完成。 - Bhargav
顺便问一下,LinearLayout 的设置是 wrap_content 吗? - Bhargav
请分享test_story_screen_layout.xml文件。 - Rajen Raiyarela
@Bhargav 谢谢,我完全不知道该怎么做,所以开始检查任何东西,甚至是post()。LinearLayout 的高度设置为 match_parent,宽度手动设置。 - forsberg
是的,您需要创建一个线性布局,将其包装在wrap_content中,并将其放置在ScrollView中,最好这样做。 - Bhargav
我做了,但是没有任何效果。为什么它可能会改变任何东西呢? - forsberg
5个回答

0

这个 Android 问题涉及到很多方面,其中一些是:

  • 在我的情况下,ll.post() 是有意义的,似乎是必需的
  • Android 中的 UI 应该以一种特殊的方式更新,而不是任何方式,例如 Async Tasks
  • 一个项目的可见性(项目应该是可见的)
  • 适当的边距(当屏幕旋转到横向时,边距似乎太大了)

该死。我认为 Android 是我见过的最没有深思熟虑的平台之一。


0

默认情况下,您的布局是水平的。当您添加新视图时,它会从屏幕的右侧边缘开始设置。只需在线性布局中将方向设置为垂直即可。


0

默认情况下,linearLayout的方向是horizontal,请将orientation设置为vertical


0
如果您想将View添加为Layout的最后一个元素,只需执行以下操作: ll.addView(tv);

谢谢,这是我第一次尝试使用只有一个参数的addView方法,然后我进一步深入研究并检查了其他方法,最终发现新的TextView只有在替换现有的TextView时才可见。在其他情况下,当它作为最后一个元素添加时,它的宽度和高度都为0(零),这可能是问题所在。无论如何,我不知道如何解决这个大���问题。 - forsberg
为什么你的布局有android:layout_width="0dp"? - Konstantin Volkov
当使用android:layout_weight="1"时,这是Google推荐的,它会自动管理视图的宽度。但这不是问题,我在其他应用程序中也使用了它。 - forsberg

0
问题在于你每次点击按钮时都用XML中定义的方式初始化线性布局 ll,因此你的线性布局会被重新创建。请将其移动。
final LinearLayout ll = (LinearLayout) findViewById(R.id.test_story_screen_layout);

如果使用Activity,就在onCreate方法中;如果使用Fragment,就在onCreateView方法中。


我做了,但似乎没有任何效果。 - forsberg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接