[英]Adding a Textview to a relative layout in Android
So I have researched everywhere but no else seems to have this problem probably because they know how to program, but anyways I'm trying to add a TextView to a Relative Layout in my Android app, but no matter what I do I cannot get the text and the buttons I already have in the layout. 因此,我到处都进行了研究,但似乎没有其他问题似乎是因为他们知道如何编程,但是无论如何我都试图将TextView添加到我的Android应用程序中的相对布局中,但是无论如何我都无法获得文字和我已经在布局中的按钮。 Here is my code for the activity:
这是我的活动代码:
public class EnterApp extends Activity {
int marblesInJar=0;
int targetMarblesInJar=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
targetMarblesInJar= intent.getIntExtra("MESSAGE_marbles",0);
RelativeLayout marble= new RelativeLayout(this);
TextView textView = new TextView(getApplicationContext());
textView.setTextSize(20);
textView.setText("Your marble jar will be full when it has "+ targetMarblesInJar + " marbles. You have " + marblesInJar + " marbles.");
textView.setTextColor(Color.BLACK);
marble.addView(textView);
this.setContentView(R.layout.activity_enter_app);
// Show the Up button in the action bar.
setupActionBar();
}
here is my code for the xml relative layout 这是我的xml相对布局代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".EnterApp" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_marginRight="16dp"
android:layout_toLeftOf="@+id/button2"
android:onClick="addMarble"
android:text="@string/addMarble" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="82dp"
android:layout_marginRight="22dp"
android:onClick="lossMarble"
android:text="@string/loseMarble" />
</RelativeLayout>
You already have the RelativeLayout
so you don't need to do 您已经有了
RelativeLayout
所以您不需要做
RelativeLayout marble= new RelativeLayout(this);
this creates a new RelativeLayout
. 这将创建一个新的
RelativeLayout
。 Just create your TextView
and add it to this layout
after you inflate
it with setContentView()
只需创建你
TextView
并将其添加到这个layout
你后inflate
与它setContentView()
public class EnterApp extends Activity {
int marblesInJar=0;
int targetMarblesInJar=0;
RelativeLayout marble;
@Override @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setContentView(R.layout.activity_enter_app);
TextView textView = new TextView(this);
textView.setTextSize(20);
textView.setText("Your marble jar will be full when it has "+ targetMarblesInJar + " marbles. You have " + marblesInJar + " marbles.");
textView.setTextColor(Color.BLACK);
marble = (RelativeLayout) findViewById(R.id.relativelayout); // id you gave your root layout
marble.addView(textView);
In your onCreate() method you have the setContentView() method which sets the activity's view to the layout file defined in it. 在onCreate()方法中,您具有setContentView()方法,该方法将活动的视图设置为其中定义的布局文件。 Right Now the parent view of your activity is the relative layout with id "relativeLayout".
现在,您的活动的父视图是ID为“ relativeLayout”的相对布局。 You need to add your textview to that relativeLayout for it to show up.
您需要将textview添加到该relativeLayout才能显示。 The code for that is as follows:
其代码如下:
RelativeLayout parent = (RelativeLayout) findViewById(R.id.relativeLayout); RelativeLayout parent =(RelativeLayout)findViewById(R.id.relativeLayout);
parent.addView(textView); parent.addView(textView);
It should show up now. 它应该现在显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.