Java 我的应用程序运行正常,但TextView赢了';我没有出现
我正在制作我的应用程序的头版。不幸的是,只有Java 我的应用程序运行正常,但TextView赢了';我没有出现,java,android,Java,Android,我正在制作我的应用程序的头版。不幸的是,只有文本视图不会显示。按钮和背景运行正常。我甚至使用addView()函数将其添加到相对布局中。有人能帮忙吗 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //layout
文本视图
不会显示。按钮和背景运行正常。我甚至使用addView()
函数将其添加到相对布局中。有人能帮忙吗
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//layout
RelativeLayout mainLayout = new RelativeLayout(this);
mainLayout.setBackgroundColor(Color.BLUE);
//button
Button mainButton = new Button (this);
mainButton.setText("Next");
mainButton.setBackgroundColor(Color.RED);
//textview
TextView mainTextView = new TextView(this);
mainTextView.setText("Juliet Daniel Lab Molecular Biology Cancer Research Center");
mainTextView.setTextSize(30);
mainTextView.setBackgroundColor(Color.GREEN);
//sizing the button
RelativeLayout.LayoutParams btnDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT
);
//sizing the textview
RelativeLayout.LayoutParams textViewDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT
);
//give id's to position relatively...note it is preferable to do this in the res folder to avoid errors
mainButton.setId(1);
mainTextView.setId(2);
//give rules to position widgets relatively
textViewDetails.addRule(RelativeLayout.ABOVE, mainButton.getId());
textViewDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
textViewDetails.setMargins(0,0,0,50);
//adding the button as a part of the RelativeLayout (ADDING THE BUTTON WIDGET TO LAYOUT)
mainLayout.addView(mainButton, btnDetails);
//adding the textview as a part of the relative Layout
mainLayout.addView(mainTextView, textViewDetails);
//setting the view as out layour
setContentView(mainLayout);
}
}
这是因为您在上面设置了
textview
按钮,而按钮是根布局的第一个顶部,这就是为什么它不显示的原因。请尝试在下面设置textview
按钮,这样做很有效
textViewDetails.addRule(RelativeLayout.BELOW, mainButton.getId());
这是因为您在上面设置了textview
按钮,而按钮是根布局的第一个顶部,这就是为什么它不显示的原因。请尝试在下面设置textview
按钮,这样做很有效
textViewDetails.addRule(RelativeLayout.BELOW, mainButton.getId());
试试穆尼尔的答案。这只是一个建议,而不是将ID设置为1,2。请使用View.generateView()方法设置ID您正在将mainTextView
对齐在main按钮上方,这是主要问题。对于mainTextView
,您应该使用main按钮的布局参数规则;不是相反,试试穆尼尔的答案。这只是一个建议,而不是将ID设置为1,2。请使用View.generateView()方法设置ID您正在将mainTextView
对齐在main按钮上方,这是主要问题。对于mainTextView
,您应该使用main按钮的布局参数规则;不是相反,谢谢你!!它起作用了。你能在这里标记一个链接让我彻底阅读和理解这个问题吗?链接什么?谢谢!!它起作用了。你能在这里标记一个链接让我彻底阅读和理解这个问题吗?链接的目的是什么?