gpt4 book ai didi

java - 从 Java 代码添加 TextView

转载 作者:行者123 更新时间:2023-12-01 11:17:31 25 4
gpt4 key购买 nike

我正在尝试通过学习 Java 和 Android Studio 来学习 Android 编程,并编写应用我所学知识的程序。不管怎样,在尝试创建一个简单的文字游戏时,我遇到了问题。我预计第 27-35 行的 for 循环会在屏幕上显示 5 个破折号,但它只显示一个。我以前使用过 Visual Studio,而且那里更简单。我缺少什么?我不确定该程序是否创建了五个 TextView 并将它们放在一处,或者只创建了一个。

这是我的 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();
}
}

最佳答案

我认为您的问题在于您为所有 TextView 重用了相同的 myTextContainer 。如果你输入这些行:

RelativeLayout.LayoutParams myTextContainer = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);

for循环内,你应该没问题。

无论如何,这是一个典型的示例,说明您应该使用线性布局(水平方向)而不是相对布局。 Android Studio 还具有布局编辑器 - 通常绘制界面比编程更容易。

关于java - 从 Java 代码添加 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31635190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com