将视图动态添加到Layout

时间:2016-02-23 11:32:09

标签: android android-layout

我很难将Android作为平台上的新手,而且据我所知,很多人也是如此。我不知道到目前为止我已经失去了多少小时 - 即使计算它们也很可怕。

我想基本上在单击按钮后向LinearLayout添加一个新的TextView(或任何其他视图)。以下是代码的这一部分:

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
}

但新添加的视图不可见(但作为子项添加到LinearLayout)。

经过几个小时检查错误后,我才发现当我更换这条线时:

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

用这个:

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

然后新的视图可见,但它取代了之前的视图 - 这是不可取的。

我已经检查了一些解决方案,例如:Refreshing a LinearLayout after adding a view

他们没有帮助我解决这个问题。

更新

线性布局看起来很好用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>

我认为没什么不寻常的。所以这两个视图(图像和文本)当然是可见的。然后我动态添加到此LinearLayout的任何新TextView(通过上面的代码中的btnClick())是不可见的,但确实添加到Layout,因为每次子视图时ll.getChildCount()都会增加添加(=点击按钮)。

为了进一步测试,我在btnClick()方法的末尾添加了这两行:

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

我猜测问题是电视(TextView)而不是ll(LinearLayout),因为tv的宽度和高度都为0。

5 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

问题是,您每次按下按钮时都会使用xml中定义的内容初始化linearlayout ll,因此您的linearlayout正在更新。移动

final LinearLayout ll = (LinearLayout) findViewById(R.id.test_story_screen_layout);
如果使用Activity,则在onCreate中使用

,如果使用Fragment,则使用onCreateView。

答案 2 :(得分:0)

默认设置为horizontal

时,linearLayout方向为orientation to vertical.

答案 3 :(得分:0)

如果你想在View中添加View作为最后一个元素,你可以这样做:  ll.addView(TV);

答案 4 :(得分:0)

这个Android问题中有很多相关内容,其中一些是:

  • ll.post()有道理并且在我的案例中被认为是必需的
  • Android中的UI应该以特殊的方式更新,而不是任何方式,它指的是例如:异步任务
  • 项目的可见性(项目应该可见)
  • 适当的边距(当屏幕例如旋转到横向时,边距看起来太大)
该死的。我发现Android是我见过的思想最少的平台之一。

相关问题