2012-07-09 58 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 2012-07-09 15:35:49

+0

Hey Luksprog,這是因爲在LinearLayout屬性中android:layout_height =「fill_parent」。我現在已經顯示文字,但在屏幕的底部。如何把它放在頂部?你幫了我。間接,但仍然。 Tnx很多。 – Majstor 2012-07-09 15:45:43

+1

如果你想把'TextView'放在某個位置,你應該使用'addView'方法的另一種方法,它接受一個int,放置新View的位置,像這樣:addView(text, 0);' – Luksprog 2012-07-09 15:49:03

回答

15

您忘記指定LayoutParameters作爲新添加的視圖。

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); 

編輯

GridView@+id/gridview的ID與的fill_parent佈局的高度定義,讓你沒有空間來添加一個新的視圖。將其高度更改爲wrap_content可能會解決您的問題。

添加我對這篇文章的評論,以幫助他人輕鬆驗證解決方案。

+0

我應該分配哪個參數?我試圖添加邊距,寬度,高度,但仍然沒有。 – Majstor 2012-07-09 15:28:02

+0

你能發佈你的mainlayout.xml嗎?因爲Arun的代碼對我也有效 – firefistace 2012-07-09 15:36:49

+0

我試過了你的代碼,但是什麼都沒有。它作爲那個父母的孩子,但沒有顯示。有更多的想法? Tnx爲你的努力。 – Majstor 2012-07-09 15:37:23

-1

我不記得了,但也許有任何「刷新」的方法來加載佈局。

Try layout.invalidate();

+0

嘗試過,仍然沒有。有更多的想法? – Majstor 2012-07-09 15:29:51

+0

@Majstor'Invalidate();'必須在頂層佈局上調用,你是否從該對象調用它? – ForceMagic 2014-02-12 21:40:45

+2

addView調用已失效。 – 2014-05-22 14:00:31

1

我有同樣的問題,並浪費了幾次找到原因。

我的錯是我添加了一個CustomView,它將match_parent設置爲高度。

我希望能節省一些寶貴的時間給任何人誰做了這個愚蠢的錯誤惱人的:)

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>