Java 如何在 Android 中将 TextView 添加到 LinearLayout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3204852/
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-13 17:24:14  来源:igfitidea点击:

How to add a TextView to LinearLayout in Android

javaandroidxmltextviewandroid-linearlayout

提问by Martin

I am trying to add TextViewsto my xml-defined layout in code. I have a xml-sheet, where a lot of Viewsare defined. But I have to add some views in code, so a create a LinearLayoutin the xml-sheet:

我试图TextViews在代码中添加到我的 xml 定义的布局。我有一个 xml 表,其中Views定义了很多。但是我必须在代码中添加一些视图,所以LinearLayout在 xml-sheet 中创建一个:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/info"
android:layout_height="wrap_content" 
android:orientation="vertical">
</LinearLayout>

And in this layout, I like to add my TextView:

在这个布局中,我喜欢添加我的TextView

    View linearLayout =  findViewById(R.id.info);
    //LinearLayout layout = (LinearLayout) findViewById(R.id.info);


    TextView valueTV = new TextView(this);
    valueTV.setText("hallo hallo");
    valueTV.setId(5);
    valueTV.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));

    ((LinearLayout) linearLayout).addView(valueTV);

But I only get the following error message:

但我只收到以下错误消息:

: java.lang.ClassCastException: android.widget.TextView

How can I do it?

我该怎么做?

Thanks for you help. Martin

谢谢你的帮助。马丁

回答by Ben

try using

尝试使用

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.info);
...
linearLayout.addView(valueTV);

also make sure that the layout params you're creating are LinearLayout.LayoutParams...

还要确保您正在创建的布局参数是 LinearLayout.LayoutParams...

回答by penguin359

You need to access the layout via it's layout resource, not an id resource which is not guaranteed unique. The resource reference should look like R.layout.my_cool_layout where your above XML layout is stored in res/layout/my_cool_layout.xml.

您需要通过布局资源访问布局,而不是不能保证唯一的 id 资源。资源引用应类似于 R.layout.my_cool_layout,其中您的上述 XML 布局存储在 res/layout/my_cool_layout.xml 中。

回答by Pushpendra Kuntal

Hey i have checked your code, there is no serious error in your code. this is complete code:

嘿,我已经检查了您的代码,您的代码中没有严重错误。这是完整的代码:

main.xml:-

main.xml:-

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

this is Stackoverflow.java

这是 Stackoverflow.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Stackoverflow extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        View linearLayout =  findViewById(R.id.info);
        //LinearLayout layout = (LinearLayout) findViewById(R.id.info);

        TextView valueTV = new TextView(this);
        valueTV.setText("hallo hallo");
        valueTV.setId(5);
        valueTV.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

        ((LinearLayout) linearLayout).addView(valueTV);
    }
}

copy this code, and run it. it is completely error free. take care...

复制这段代码,然后运行它。它完全没有错误。小心...

回答by Deepak

You can add a TextViewto your linear layout programmatically like this:

您可以TextView像这样以编程方式将 a 添加到您的线性布局:

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mylayout);
TextView txt1 = new TextView(MyClass.this);
linearLayout.setBackgroundColor(Color.TRANSPARENT);
linearLayout.addView(txt1);

回答by Zapnologica

LinearLayout.LayoutParams layoutParams ;
layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

回答by user3509153

Here's where the exception occurs

这是发生异常的地方

((LinearLayout) linearLayout).addView(valueTV);

addViewmethod takes in a parameter of type View, not TextView. Therefore, typecast the valueTv object into a View object, explicitly.

addView方法接受类型为 的参数View,而不是TextView. 因此,明确地将 valueTv 对象类型转换为 View 对象。

Therefore, the corrected code would be :

因此,更正后的代码将是:

((LinearLayout) linearLayout).addView((TextView)valueTV);

回答by vaibhav jain

for(int j=0;j<30;j++) {
    LinearLayout childLayout = new LinearLayout(MainActivity.this);
    LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);
    childLayout.setLayoutParams(linearParams);

    TextView mType = new TextView(MainActivity.this);
    TextView mValue = new TextView(MainActivity.this);

    mType.setLayoutParams(new TableLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT, 1f));
    mValue.setLayoutParams(new TableLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT, 1f));

    mType.setTextSize(17);
    mType.setPadding(5, 3, 0, 3);
    mType.setTypeface(Typeface.DEFAULT_BOLD);
    mType.setGravity(Gravity.LEFT | Gravity.CENTER);

    mValue.setTextSize(16);
    mValue.setPadding(5, 3, 0, 3);
    mValue.setTypeface(null, Typeface.ITALIC);
    mValue.setGravity(Gravity.LEFT | Gravity.CENTER);

    mType.setText("111");
    mValue.setText("111");

    childLayout.addView(mValue, 0);
    childLayout.addView(mType, 0);

    linear.addView(childLayout);
}

回答by Ntheitroad

You should use something similar to this for adding TextView to LinearLayout dynamically:

您应该使用类似于此的内容将 TextView 动态添加到 LinearLayout:

LinearLayout linearLayout = getActivity().findViewById(R.id.infoLayout);

TextView valueTV = new TextView(context);
valueTV.setText("hallo hallo");
valueTV.setId(Integer.parseInt("5"));
valueTV.setLayoutParams(new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.FILL_PARENT,
       LinearLayout.LayoutParams.WRAP_CONTENT));

linearLayout.addView(valueTV);

getActivity() is used for inside Fragments, you can use context or anything similar per each instance you are inside.

getActivity() 用于内部 Fragment,您可以为每个内部实例使用上下文或任何类似的东西。