从 Java 代码添加 TextView
Adding TextViews from Java code
我正在尝试通过学习 Java 和 Android Studio 来学习 Android 编程,并且我编写程序来应用我所学的知识。无论如何,试图创建一个简单的文字游戏,我 运行 遇到了问题。我希望第 27-35 行中的 for 循环在屏幕上显示 5 个破折号,但它只显示一个。我以前用过 Visual Studio 好吧,那里更简单。我错过了什么?我不确定该程序是创建了五个文本视图并将它们放在一个地方还是只创建了一个。
这是我的 MainActivity.java:
package com.mycompany.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.graphics.Color;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//The GUI related codes and definitions are here
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainGame meGuess = new MainGame();
final String dashForTextFields = "-";
meGuess.setGuessingWord();
RelativeLayout mainGameLayout = (RelativeLayout) findViewById(R.id.myLayout);
RelativeLayout.LayoutParams myTextContainer = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView[] myTextViews = new TextView[meGuess.getGuessingWordLength()];
for(int i = 0; i < meGuess.getGuessingWordLength(); i++)
{
myTextViews[i] = new TextView(this);
myTextViews[i].setText(dashForTextFields);
myTextViews[i].setTextSize(100);
myTextContainer.leftMargin = 10 * i;
myTextContainer.topMargin = 50;
mainGameLayout.addView(myTextViews[i], myTextContainer);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and here's MainGame class:
package com.mycompany.myapplication;
public class MainGame
{
private String wordToBeGuessed;
public String getGuessingWord()
{
return wordToBeGuessed;
}
public void setGuessingWord()
{
//IMPORTANT NOTE: This method should be updated//
wordToBeGuessed = "apple";
}
public int getGuessingWordLength()
{
return wordToBeGuessed.length();
}
}
我认为您的问题在于您对所有文本视图重复使用相同的 myTextContainer
。如果你输入这些行:
RelativeLayout.LayoutParams myTextContainer = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
在 for
循环中,你应该没问题。
无论如何,这是您应该使用线性布局(水平方向)而不是相对布局的典型示例。 Android Studio 也有一个布局编辑器 - 绘制 界面通常比 编程 它们更容易。
我正在尝试通过学习 Java 和 Android Studio 来学习 Android 编程,并且我编写程序来应用我所学的知识。无论如何,试图创建一个简单的文字游戏,我 运行 遇到了问题。我希望第 27-35 行中的 for 循环在屏幕上显示 5 个破折号,但它只显示一个。我以前用过 Visual Studio 好吧,那里更简单。我错过了什么?我不确定该程序是创建了五个文本视图并将它们放在一个地方还是只创建了一个。
这是我的 MainActivity.java:
package com.mycompany.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.graphics.Color;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//The GUI related codes and definitions are here
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainGame meGuess = new MainGame();
final String dashForTextFields = "-";
meGuess.setGuessingWord();
RelativeLayout mainGameLayout = (RelativeLayout) findViewById(R.id.myLayout);
RelativeLayout.LayoutParams myTextContainer = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView[] myTextViews = new TextView[meGuess.getGuessingWordLength()];
for(int i = 0; i < meGuess.getGuessingWordLength(); i++)
{
myTextViews[i] = new TextView(this);
myTextViews[i].setText(dashForTextFields);
myTextViews[i].setTextSize(100);
myTextContainer.leftMargin = 10 * i;
myTextContainer.topMargin = 50;
mainGameLayout.addView(myTextViews[i], myTextContainer);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and here's MainGame class:
package com.mycompany.myapplication;
public class MainGame
{
private String wordToBeGuessed;
public String getGuessingWord()
{
return wordToBeGuessed;
}
public void setGuessingWord()
{
//IMPORTANT NOTE: This method should be updated//
wordToBeGuessed = "apple";
}
public int getGuessingWordLength()
{
return wordToBeGuessed.length();
}
}
我认为您的问题在于您对所有文本视图重复使用相同的 myTextContainer
。如果你输入这些行:
RelativeLayout.LayoutParams myTextContainer = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
在 for
循环中,你应该没问题。
无论如何,这是您应该使用线性布局(水平方向)而不是相对布局的典型示例。 Android Studio 也有一个布局编辑器 - 绘制 界面通常比 编程 它们更容易。