おそらくすべての投稿とドキュメントを読みましたが、まだこの問題を解決できません。
メソッドを使用して既存の (実行中の) レイアウトにビューを追加したいのですaddView()
が、何らかの理由でできません。これは簡単で基本的なことであることはわかっていますが、それでもできません。だから、私を助けてください。
コードは次のとおりです。
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
TextView text = new TextView(this);
text.setText("test");
layout.addView(text);
これはコードであり、その結果、XML ファイルで定義されたビューのみが表示されました。私が追加したこの新しいビューはありません。デバッグすると、この追加されたビューが、追加した親の子として表示されますが、表示されません。
ここにmain.xmlがあります:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/main1" >
<TextView android:id="@+id/app_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="@string/app_title"
android:textSize="25dp"
android:gravity="center_horizontal"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/main_screen_counter_title"
android:textSize="15dp"
android:textColor="#FFF"
android:gravity="center_horizontal"/>
<TextView android:id="@+id/frontScreenCounter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="@string/reading"
android:textSize="33dp"
android:gravity="center_horizontal" />
<GridView android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:textColor="#888"
/>
</LinearLayout>
助けてください。これは私を夢中にさせます!