2012-07-09 42 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>