2012-07-09 9 views
15

おそらくすべての投稿とドキュメントを読んでいますが、この問題は解決できません。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> 

助けてください。これは私にナッツを運ぶでしょう!

+0

あなたは 'TextView'を追加したいレイアウトを追加できますか? – Luksprog

+0

Hey Luksprog、それはLinearLayout属性のアンドロイド:layout_height = "fill_parent"が原因でした。私は今テキストを表示していますが、画面の一番下にあります。それをトップに置く方法は?あなたは私を助けました。間接的だが、依然として。たくさんのTnx。 – Majstor

+1

その 'TextView'を特定の位置に置くには、新しい' View'を置く位置であるintをとる 'addView'メソッドの別の風味を使うべきです:' addView(text、 0); ' – Luksprog

答えて

15

新しく追加されたビューにはを指定していません。 @+id/gridviewのIDを持つ

LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout); 
    TextView text=new TextView(this); 
    text.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    text.setText("test"); 
    layout.addView(text); 

EDIT

GridViewは、新しいビューを追加するためにスペースなしであなたを残して、fill_parentのレイアウト高さで定義されています。高さをwrap_contentに変更すると、問題が解決する場合があります。

この記事に自分のコメントを追加すると、他の人が簡単に解決策を確認できるようになります。

+0

どのパラメータを割り当てる必要がありますか?余白、幅、高さを追加しようとしましたが、まだ何もありません。 – Majstor

+0

mainlayout.xmlを投稿できますか? Arunのコードも私のために働いているので – firefistace

+0

あなたのコードを試してみましたが、まだ何もしていません。その親の子としてそこに表示されません。より多くのアイデアがありますかあなたの努力のためにTnx。 – Majstor

-1

私は覚えがたいですが、レイアウトを再度読み込むための「リフレッシュ」メソッドがあります。

try layout.invalidate();

+0

まだ、何も試していません。より多くのアイデアがありますか – Majstor

+0

@Majstor 'Invalidate();'はトップレベルのレイアウトで呼び出されなければなりません。そのオブジェクトから呼び出されましたか? – ForceMagic

+2

addView呼び出しが既に無効になっています。 –

1

私は同じ問題を抱えており、その理由を見いだすのに数時間を費やしました。

私の欠点は、match_parentを高さに設定したCustomViewを追加していたことです。

私はこの愚かな迷惑な間違いをした誰にもいくつかの貴重な時間を節約するために願っています:)

TextView tv = new TextView(this); 
lytMarks.addView(tv); 


CustomView cv = new CustomView(this, someObject); 
lytMarks.addView(cv); // <-- This one had height = match_parent!! 


EditText et = new EditText(this); 
lytMarks.addView(et); 
0

あなたのLinearLayoutのコンテンツは親のビューの上にあります。 ScrollViewを使用する必要があります。 このように:

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<LinearLayout 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/main1" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/app_title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="@string/app_title" 
     android:textColor="#FFF" 
     android:textSize="25dp" /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:gravity="center_horizontal" 
     android:text="@string/main_screen_counter_title" 
     android:textColor="#FFF" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/frontScreenCounter" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="@string/reading" 
     android:textColor="#FFF" 
     android:textSize="33dp" /> 

    <GridView 
     android:id="@+id/gridview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:columnWidth="90dp" 
     android:gravity="center" 
     android:horizontalSpacing="10dp" 
     android:numColumns="3" 
     android:stretchMode="columnWidth" 
     android:textColor="#888" 
     android:verticalSpacing="10dp" /> 
</LinearLayout> 
</ScrollView> 
関連する問題