猿问

Android:以编程方式将textview添加到线性布局

我试图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>

在这个布局中,我想添加我的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);

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


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

我该怎么做?



温温酱
浏览 594回答 3
3回答
随时随地看视频慕课网APP

相关分类

Java
Android
我要回答