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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11398103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:16:11  来源:igfitidea点击:

How to use addView to add view to layout?

androidlayoutviewviewgroupadview

提问by Majstor

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

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

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.

我想使用addView()方法将视图添加到现有(正在运行)布局中,但由于某种原因我不能。我知道这应该是简单和基本的,但我仍然无法做到。所以,请帮帮我。

Here is a code:

这是一个代码:

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

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.

这是一个代码,结果是我只显示了在 XML 文件中定义的视图。我没有添加这个新视图。当我调试时,我看到这个添加的视图是我添加它的父级的子级,但它没有显示。

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

Please help. This will driving me nuts!

请帮忙。这会让我发疯!

采纳答案by Arun George

You forgot to specify the LayoutParametersfor the newly added view.

您忘记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);

EDIT

编辑

The GridViewwith an id of @+id/gridviewis defined with a layout height of fill_parent, leaving you with no space to add a new view. Changing its height to wrap_contentmay solve your problem.

GridView用的ID@+id/gridview与布局高度定义的fill_parent,让你没有空间来添加一个新的视图。将其高度更改为wrap_content可能会解决您的问题。

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

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

回答by voghDev

I had the same problem and wasted several time finding the reason.

我遇到了同样的问题,浪费了很多时间寻找原因。

My fault was that I was adding a CustomView that had match_parent set as height.

我的错是我添加了一个将 match_parent 设置为高度的 CustomView。

I hope to save some precious time to anybody who did this silly annoying mistake :)

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

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

回答by peter zhang

Your linearLayout content is over the parent's View. so you need to use ScrollView. like this:

您的 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>

回答by Fustigador

I can't remember, but maybe there is any "refresh" method to load again the layout.

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

Try layout.invalidate();

尝试 layout.invalidate();