Android 如何使用addView将视图添加到布局?

Android 如何使用addView将视图添加到布局?,android,layout,view,viewgroup,adview,Android,Layout,View,Viewgroup,Adview,我可能已经阅读了所有的帖子和文档,但我仍然无法解决这个问题 我想使用addView()方法将视图添加到现有(正在运行的)布局中,但由于某些原因,我不能。我知道这应该很简单,但我还是做不到。所以,请帮帮我 下面是一个代码: LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout); TextView text = new TextView(this); text.setText("test"); layout.a

我可能已经阅读了所有的帖子和文档,但我仍然无法解决这个问题

我想使用
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>


请帮忙。这会让我发疯的

我不记得了,但是可能有任何“刷新”方法可以再次加载布局


尝试layout.invalidate()

您忘记为新添加的视图指定
布局参数

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


id为
@+id/GridView
GridView
定义为布局高度为
fill\u parent
,没有空间添加新视图。将其高度更改为
wrap\u content
可以解决您的问题


将我的评论添加到此帖子,以帮助其他人轻松验证解决方案。

我也遇到了同样的问题,并浪费了几次时间寻找原因

我的错误是,我添加的CustomView将match_parent设置为height

我希望能为犯了这个愚蠢而恼人的错误的人节省一些宝贵的时间:)

TextView电视=新的TextView(本);
lytMarks.addView(电视);
CustomView cv=新CustomView(此,someObject);

lytMarks.addView(cv);// 您的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>


我应该分配哪些参数?我试着添加边距、宽度和高度,但仍然没有。你能发布mainlayout.xml吗?因为阿伦的代码对我有用,所以我试过你的代码,但还是一无所获。它作为父项的子项存在,但不显示。有更多的想法吗?感谢您的努力。使用您提供的代码,这是我能想到的唯一解决方案。如果您发布更多代码,可能会发现错误。id为
@+id/GridView
GridView
的布局高度定义为
fill\u parent
,因此您没有空间添加新视图。将其高度更改为
wrap\u content
可能会解决您的问题。已尝试,但仍然无效。有更多想法吗?@Majstor the
Invalidate(),您是从该对象调用它的吗?您可以在要添加
TextView
的位置添加布局吗?嘿,Luksprog,这是因为LinearLayout属性中的android:layout\u height=“fill\u parent”。我现在已经在屏幕底部显示了文本。如何把它放在顶部?你帮助了我。间接地,但仍然是。Tnx很多。如果你想把
TextView
放在某个位置,你应该使用另一种
addView
方法,该方法使用int,即放置新
视图的位置,类似这样:
addView(text,0)完成!就这样!Tnx又多了一次。速度很快。当心。
<?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>