简体   繁体   English

Android:如何调整Textview

[英]Android : How to adjust Textview

Hello i am trying to display the content i receive in an activity using TextView but it seems that TextView is overlapping a button that i have put in activity's UI.My goal is to put TextView and the button side by side. 您好,我试图显示我在使用TextView的活动中收到的内容,但似乎TextView与我在活动的UI中放置的按钮重叠。我的目标是将TextView和该按钮并排放置。 I have put the TextView in the UI dynamically like this: 我已经将TextView动态地放置在UI中,如下所示:

String display = extras.getString("EXTRA_MESSAGE");


    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setWidth(20);
    textView.setHeight(20);
    textView.setText(display);

    setContentView(textView);

I know i miss something but i cant find what it is,so can you please suggest a way how to fix that? 我知道我想念某些东西,但我找不到它,所以请您提出一种解决方法?

Thanks a lot in advance! 在此先多谢!

When you call setContentView(textView); 当您调用setContentView(textView); it changes your layout to just the textView so it isn't overlapping your Button but your Button isn't shown anymore. 它会将您的layout更改为仅textView因此它不会与Button重叠,但是您的Button不再显示。 You need to add it to your layout and put it where you want it. 您需要将其添加到layout ,并将其放置在所需的位置。

You can do this by getting a reference to your root View in your xml and calling addView(textVie) on that root View and use addRule() to position your TextView where you want. 您可以通过在xml中获取对根View的引用并在该根View上调用addView(textVie)并使用addRule()TextView放置在所需位置的方式来实现。 However, if it isn't necessary to add your TextView dynamically then it is much easier to declare it in your xml. 但是,如果不需要动态添加TextView ,则在xml中声明它要容易得多。

If you do want to add it dynamically still, then this SO answer , and many more, covers it. 如果您确实想仍然动态添加它,那么这个SO答案以及更多内容都可以解决。

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

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