2016-08-03 23 views
-2

编辑:我错了,谢谢@Amylinn的帮助。Android的addView到LinearLayout不起作用。怎么了?

获得的经验:不要忘记在假设某些事情出错之前运行您的代码。我道歉。

-

我花了4个小时在谷歌和堆栈溢出。我试图通过Java将TextView添加到LinearLayout。我不明白我的代码有什么问题,我在堆栈溢出上尝试了一些解决方案,并在其上添加了绿色复选标记,但代码仍然不起作用。有人能帮我吗?

这是我的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/root_of_numbers_activity" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     tools:context=".NumbersActivity"> 
</LinearLayout> 

我曾尝试从match_parentlayout_height值更改为wrap_content,改变它的方向,仍然没有工作。下面是情况下的截图,如果你需要它(我使用的IntelliJ IDEA,我还没有在Android Studio的测试):

Note: The Rendering Problem warning always there, even if I added the TextView directly to XML file, the code works fine and so does the preview. I wonder if this is the problem?

-

而这里的Java文件:

package com.example.android.miwok; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

import java.util.ArrayList; 

public class NumbersActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_numbers); 

     ArrayList<String> list_words = new ArrayList<String>(); 
     list_words.add("Hello!"); 



//========CODE NUMBER 1 STARTS======== 
//  LinearLayout rootOfNumbersActivity = (LinearLayout)findViewById(R.id.root_of_numbers_activity); 
//  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
//    LinearLayout.LayoutParams.WRAP_CONTENT, 
//    LinearLayout.LayoutParams.WRAP_CONTENT 
//  ); 
//  TextView numbersList = new TextView(this); 
//  numbersList.setLayoutParams(layoutParams); 
//  numbersList.setText(list_words.get(0)); 
// 
//  rootOfNumbersActivity.addView(numbersList); 
//========CODE NUMBER 1 ENDS======== 



//========CODE NUMBER 2 STARTS======== 
//  LinearLayout rootOfNumbersActivity = (LinearLayout)findViewById(R.id.root_of_numbers_activity); 
//  TextView numbersList = new TextView(this); 
//  numbersList.setLayoutParams(new LinearLayout.LayoutParams(
//    LinearLayout.LayoutParams.WRAP_CONTENT, 
//    LinearLayout.LayoutParams.WRAP_CONTENT)); 
//  numbersList.setText(list_words.get(0)); 
//  rootOfNumbersActivity.addView(numbersList); 
//========CODE NUMBER 2 ENDS======== 



//========CODE NUMBER 3 STARTS======== 
     ViewGroup rootOfNumbersActivity = (ViewGroup)findViewById(R.id.root_of_numbers_activity); 
     TextView numbersList = new TextView(this); 
     numbersList.setLayoutParams(new ViewGroup.LayoutParams(
       ViewGroup.LayoutParams.WRAP_CONTENT, 
       ViewGroup.LayoutParams.WRAP_CONTENT)); 
     numbersList.setText(list_words.get(0)); 
     rootOfNumbersActivity.addView(numbersList); 
//========CODE NUMBER 3 ENDS======== 
    } 
} 

注意:我已经在上面的注释中尝试了代码,不工作。

-

这里就是我想要做的:我要添加视图,TextViewLinearLayout,通过Java文件。我是Android开发新手,请尽可能清楚地解释。如果您需要其他线索,请告诉我。最后,感谢你的努力和你的时间。 : - )

P.S.英语是我的第三语言,我对语法错误表示歉意。

+0

你有什么错误吗? – yennsarah

+0

@Amylinn no Amylinn,在XML和Java文件中没有错误警告。 –

+0

您是否尝试过运行它?你的屏幕上有什么输出? – yennsarah

回答

0

由于您的所有三个代码示例看起来都不错,因此请忽略警告,只需尝试启动它即可生成项目&。预览中未显示编程添加的Views

我会建议使用你的第一个或第二个例子,并将你的LinearLayouts高度设置为android:layout_height="match_parent"

+0

为什么'android:layout_height =“match_parent”'?我也读过Stack Overflow的问题,他的代码不起作用,因为'match_parent'值正在推动他编程添加'TextView'。 –

+0

由于'LinearLayout'是您的根布局,因此它应该与屏幕大小相匹配。它不必,但我认为这是最常用的做法。如果'LinearLayout'不是根布局,如果它的高度设置为'match_parent',它可以将其他'Views'从屏幕上移开。 – yennsarah

+0

啊,我明白了。你能帮我理解这段代码的作用:'TextView numbersList = new TextView(this)'。假设此代码位于MainActivity.java上,那么'this'关键字指向什么? –

相关问题