如何使用addView将视图添加到布局? [英] How to use addView to add view to layout?

查看:89
本文介绍了如何使用addView将视图添加到布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have read probably all posts and documentation but I still can't solve this issue.

我想使用addView()方法将视图添加到现有(运行)布局中,但是由于某些原因,我不能.我知道这应该很简单并且很基本,但是我仍然做不到.所以,请帮助我.

I want to use addView() method to add view to the existing (running) layout but for some reason I cant. I know that this should be easy and basic but still I cant do it. So, please help me.

这是一个代码:

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

那是代码,结果是我只显示了XML文件中定义的视图.我没有添加此新视图. 调试时,我看到此添加的视图是添加了该视图的父视图的子视图,但未显示.

That's a code, and the result is that I have displayed only views which are defined in XML file. There is no this new view I have added. When I debug I see this added view as a child of the parent to which I have added it but it isn't displayed.

这是main.xml:

here is 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>

请帮助.这会让我发疯!

Please help. This will driving me nuts!

推荐答案

您忘记为新添加的视图指定LayoutParameters.

You forgot to specify the LayoutParameters for the newly added view.

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/gridviewGridView的布局高度为fill_parent,使您没有空间添加新视图.将其高度更改为wrap_content可能会解决您的问题.

The GridView with an id of @+id/gridview is defined with a layout height of fill_parent, leaving you with no space to add a new view. Changing its height to wrap_content may solve your problem.

在此信息中添加我的评论,以帮助其他人轻松地验证解决方案.

Adding my comment to this post to help others easily verify the solution.

这篇关于如何使用addView将视图添加到布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆