简体   繁体   English

如何使由setContentView()设置的textview(具有透明背景)中的“文本”显示在activity.xml中的布局上方?

[英]How do I make a “text” from textview(with a transparent background) set by setContentView( ) be displayed above my layout in activity.xml?

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.countdown_main);
    // Show the Up button in the action bar.
    setupActionBar();

    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Get the message from intent
    Intent intent1 = getIntent();

    // Create the text view
    TextView textView1 = new TextView(this);
    textView1.setTextSize(45);
    textView1.setText(message);
    setContentView(R.layout.countdown_main);

    // Set the text view as an activity layout
    setContentView(textView1);


}

Why is it that my view from countdown_main.xml isn't being shown directly underneath the setcontentview() ? 为什么我的countdown_main.xml视图没有显示在setcontentview()的正下方?

I would like the text in textview1 to be shown in the middle of my activity screen on top of my layout in countdown_main.xml . 我希望textview1中的文本在countdown_main.xml布局顶部的活动屏幕中间显示。

How do I do this? 我该怎么做呢?

SetContentView sets the entire view for your app. SetContentView为您的应用设置整个视图。 It replaces the previous view. 它替换了先前的视图。 If you want to add a view to the screen, the best way would be to put that view in your xml. 如果要在屏幕上添加视图,最好的方法是将该视图放入xml中。 If you really wanted to add it dynamically, you'd find a ViewGroup (like a LinearLayout or a RelativeLayout) in your xml via findViewById. 如果您真的想动态添加它,则可以通过findViewById在xml中找到一个ViewGroup(如LinearLayout或RelativeLayout)。 Then you would add the new view to it as a child view. 然后,您可以将新视图添加为子视图。 Make sure to set its layout parameters as well. 确保还设置其布局参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM