おそらくすべての投稿とドキュメントを読んでいますが、この問題は解決できません。addViewが機能しない
私は既存の(実行中の)レイアウトにビューを追加するために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>
助けてください。これは私にナッツを運ぶでしょう!
あなたは 'TextView'を追加したいレイアウトを追加できますか? – Luksprog
Hey Luksprog、それはLinearLayout属性のアンドロイド:layout_height = "fill_parent"が原因でした。私は今テキストを表示していますが、画面の一番下にあります。それをトップに置く方法は?あなたは私を助けました。間接的だが、依然として。たくさんのTnx。 – Majstor
その 'TextView'を特定の位置に置くには、新しい' View'を置く位置であるintをとる 'addView'メソッドの別の風味を使うべきです:' addView(text、 0); ' – Luksprog