2017-05-03 55 views
0

我想将TextViews充气到LinearLayout。将TextViews充气到LinearLayout

我有一个布局activity_play_game和java类PlayGameActivity。 目前,TextViews的硬编码数,它看起来像这样:

enter image description here

现在我试图根据变量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的方法。不幸的是,现在它看起来像这样:

enter image description here

正如你可以看到它在底部添加TextViews。所以我删除了所有TextViews从activity_play_game和添加的LinearLayout膨胀TextViews:

enter image description here

我怎么能膨胀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。你知道该怎么做吗?

+0

你不能在'LinearLayout'内膨胀'TextView',你可以用'addView'将它添加到'LinearLayout'中 –

回答

1

获取您的LinearLayoutcontainer_blue的参考号并将其添加TextView's即可。

试试这个:

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; 
} 
+0

年,它工作正常!我玩过: LinearLayout playGame =(LinearLayout)findViewById(R.id.activity_play_game);它应该是: LinearLayout playGame =(LinearLayout)findViewById(R.id.container_blue); 只是不同的布局:) 非常感谢:) – Tomas

+0

我的荣幸:) ... – FAT

1

使用的TextView这样做一个布局文件:

layout_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"/> 

然后应用按照你的java文件中的代码:

LinearLayout playGame = (LinearLayout) findViewById(R.id.container_blue);  
LayoutInflater li =(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View view = li.inflate(R.layout.layout_textview, null); 
TextView txt= (TextView) view.findViewById(R.id.txt_spin); 
playGame.addView(view); 

希望它为你工作...编码快乐.. :)

0

至于我看到你最初的问题是关于三个按钮之前动态地添加的项目。如果您想在三个按钮之前添加它们,只需拨打playGame.addView(temp,0);这将始终将项目添加到布局中的第一个位置。 如果您需要添加视图的特定顺序,您需要使用for循环以正确顺序添加视图。