gpt4 book ai didi

java - 我的应用程序运行良好,但 TextView 不会显示

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

我正在制作我的应用程序的首页。不幸的是,只有 TextView 不会显示。按钮和后台运行得很好。我什至使用 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,其中 button 是根布局的第一个顶部,这就是为什么它没有显示尝试在下面设置 textview按钮就可以了

textViewDetails.addRule(RelativeLayout.BELOW, mainButton.getId());

关于java - 我的应用程序运行良好,但 TextView 不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47989705/

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