从Java代码添加TextViews

时间:2015-07-26 08:47:53

标签: java android android-widget android-relativelayout

我正在尝试通过学习Java和Android Studio来学习Android编程,并且我制作了应用我学到的东西的程序。无论如何,试图创建一个简单的文字游戏,我遇到了问题。我希望我在第27-35行的循环在屏幕上显示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();
        }
}

1 个答案:

答案 0 :(得分:1)

我认为您的问题在于您为所有文本视图重复使用相同的myTextContainer。如果你把这些行:

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

里面 for - 循环,你应该没问题。

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