当前位置: 代码迷 >> java >> 从Java代码添加TextViews
  详细解决方案

从Java代码添加TextViews

热度:124   发布时间:2023-07-16 17:42:05.0

我正在尝试通过学习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();
        }
}

我认为您的问题出在所有文本视图都使用相同的myTextContainer 如果您输入以下行:

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

for循环中,您应该没问题。

无论如何,这是一个典型的示例,您应在其中使用线性布局(水平方向)而不是相对布局。 和Android工作室也有布局编辑器-这是通常更容易吸引接口,而不是它们编程

  相关解决方案