我想将TextViews充气到LinearLayout。将TextViews充气到LinearLayout
我有一个布局activity_play_game
和java类PlayGameActivity
。 目前,TextViews的硬编码数,它看起来像这样:
现在我试图根据变量numberForbiddenWords
的价值添加TextViews的数量。我加了一段代码,看起来像这样:
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;
}
而且我有增加textViews到activity_play_game
的方法。不幸的是,现在它看起来像这样:
正如你可以看到它在底部添加TextViews。所以我删除了所有TextViews从activity_play_game
和添加的LinearLayout膨胀TextViews:
我怎么能膨胀TextViews本的LinearLayout上面的扣子? 我发现一种解决方案:
activity_to_add_words:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
在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);
但这是一个View
,不TextView
。你知道该怎么做吗?
你不能在'LinearLayout'内膨胀'TextView',你可以用'addView'将它添加到'LinearLayout'中 –