Java 将文本视图充气到线性布局

Java 将文本视图充气到线性布局,java,android,android-inflate,Java,Android,Android Inflate,我想将文本视图膨胀为线性布局 我有一个布局activity\u play\u game和java类PlayGameActivity。 目前,对于文本视图的硬编码数量,它如下所示: LinearLayout playGame = (LinearLayout) findViewById(R.id.activity_play_game); TextView temp; TextView[] textViews = new TextView[numberForbiddenWords]; for(int

我想将文本视图膨胀为线性布局

我有一个布局
activity\u play\u game
和java类
PlayGameActivity
。 目前,对于文本视图的硬编码数量,它如下所示:

LinearLayout playGame = (LinearLayout) findViewById(R.id.activity_play_game);
TextView temp;
TextView[] textViews = new TextView[numberForbiddenWords];
for(int i = 0; i < numberForbiddenWords; i++) {
    temp = new TextView(this);
    playGame.addView(temp);
    textViews[i] = temp;
}

现在我尝试根据变量
numberForbiddenWords
的值添加文本视图的数量。我添加了一段代码,如下所示:

LinearLayout playGame = (LinearLayout) findViewById(R.id.activity_play_game);
TextView temp;
TextView[] textViews = new TextView[numberForbiddenWords];
for(int i = 0; i < numberForbiddenWords; i++) {
    temp = new TextView(this);
    playGame.addView(temp);
    textViews[i] = temp;
}

在java类中:

final View addView;
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
addView = layoutInflater.inflate(R.layout.activity_to_add_words,
        null);
final TextView textOut = (TextView) addView.findViewById(R.id.textout);

但这是针对
视图
,而不是
文本视图
。你知道怎么做吗?

获取你的
线性布局的参考
容器的蓝色
并将
文本视图的
添加到其中

试试这个:

LinearLayout playGame = (LinearLayout) findViewById(R.id.container_blue);
TextView temp;
TextView[] textViews = new TextView[numberForbiddenWords];
for(int i = 0; i < numberForbiddenWords; i++) {
    temp = new TextView(this);
    temp.setText("TextView " + String.valueOf(i));
    temp.setId(i);
    temp.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

    playGame.addView(temp);
    textViews[i] = temp;
}
LinearLayout playGame=(LinearLayout)findViewById(R.id.container_blue);
文本视图温度;
TextView[]TextView=新的TextView[numberForbiddenWords];
for(int i=0;i
获取您的
线性布局的参考
容器的蓝色
并将
文本视图的
添加到其中

试试这个:

LinearLayout playGame = (LinearLayout) findViewById(R.id.container_blue);
TextView temp;
TextView[] textViews = new TextView[numberForbiddenWords];
for(int i = 0; i < numberForbiddenWords; i++) {
    temp = new TextView(this);
    temp.setText("TextView " + String.valueOf(i));
    temp.setId(i);
    temp.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

    playGame.addView(temp);
    textViews[i] = temp;
}
LinearLayout playGame=(LinearLayout)findViewById(R.id.container_blue);
文本视图温度;
TextView[]TextView=新的TextView[numberForbiddenWords];
for(int i=0;i
使用textview制作一个布局文件,如下所示:

layout\u textview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/txt_spin"
android:textSize="18sp"
android:background="#fff"
android:padding="5dp"
android:textColor="#000"
android:layout_height="wrap_content"/>

希望它对您有用…快乐编码:)

使用textview制作一个布局文件,如下所示:

layout\u textview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/txt_spin"
android:textSize="18sp"
android:background="#fff"
android:padding="5dp"
android:textColor="#000"
android:layout_height="wrap_content"/>

希望它对您有用…快乐编码:)

据我所知,您最初的问题是在三个按钮之前动态添加项目。如果要在三个按钮之前添加它们,只需调用
playGame.addView(temp,0)这将始终将项目添加到布局中的第一个位置。

如果您需要添加视图的特定顺序,则需要使用for循环以正确的顺序添加它们。

据我所知,您最初的问题是在三个按钮之前动态添加项目。如果要在三个按钮之前添加它们,只需调用
playGame.addView(temp,0)这将始终将项目添加到布局中的第一个位置。

如果您需要添加视图的特定顺序,您需要使用for循环以正确的顺序添加它们。

您不能充气
TextView
内部
LinearLayout
您可以使用
addView
将其添加到
LinearLayout
您不能充气
TextView
内部
LinearLayout
您可以添加它使用
addView
Year来
LinearLayout
,它正在工作!我有:linearlayoutplaygame=(LinearLayout)findViewById(R.id.activity\u play\u game);但它应该是:linearlayoutplaygame=(LinearLayout)findViewById(R.id.container_blue);只是不同的布局:)非常感谢:)今年,它的工作!我有:linearlayoutplaygame=(LinearLayout)findViewById(R.id.activity\u play\u game);但它应该是:linearlayoutplaygame=(LinearLayout)findViewById(R.id.container_blue);只是不同的布局:)非常感谢:)