簡體   English   中英

以編程方式生成textview時,不會出現靜態視圖

[英]When I programmatically generate textviews, static ones don't appear

我有這樣的布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
        android:orientation="vertical"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_goster" tools:context="com.example.GosterActivity">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text_line1"
    android:layout_centerHorizontal="true"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text_line2"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/text_line1"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rl_root"
        android:layout_below="@+id/text_line2">

    </LinearLayout>

</LinearLayout>
</ScrollView>

在Java中,我這樣設置文本:

tvline1.setText("asdf1");
tvline1.setText("asdf2");

我以編程方式在rl_root中生成TextViews,如下所示:

LinearLayout relativeLayout2 = (LinearLayout) findViewById(R.id.rl_root);

for(int i = 0; i < 5; i++)
{
  TextView textvw = new TextView(this);
  textvw.setText(Integer.toString(i));
  relativeLayout2.addView(textvw);
  for(int j = 0; j < 10; j++)
  {
    TextView textvw2 = new TextView(this);
    textvw2 .setText(Integer.toString(j+5));
    relativeLayout2.addView(textvw2);
  }
}

使用這種配置,我只能看到生成的textview的內容,而不是我手工編寫的前兩個視圖。 您能告訴我如何顯示這兩個TextView嗎? 謝謝。

我能夠獲得兩種文本視圖-生成的文本視圖和手寫的文本視圖。 代碼似乎很好。 我剛剛刪除了這些行

tools:showIn="@layout/activity_goster" 
tools:context="com.example.GosterActivity"

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM