0

所以我到处研究过,但似乎没有其他人有这个问题,可能是因为他们知道如何编程,但无论如何我试图在我的 Android 应用程序中将 TextView 添加到相对布局中,但无论我做什么我都无法得到文本和布局中已有的按钮。这是我的活动代码:

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();
}

这是我的 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>
4

2 回答 2

0

你已经有了,RelativeLayout所以你不需要做

 RelativeLayout marble= new RelativeLayout(this);

这会创建一个新的RelativeLayout. 只需创建您的TextView并将其添加到layoutinflate之后setContentView()

public class EnterApp extends Activity {
    int marblesInJar=0;
    int targetMarblesInJar=0;
    RelativeLayout marble;

@覆盖

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);
于 2013-08-13T01:02:18.103 回答
0

在您的 onCreate() 方法中,您有 setContentView() 方法,它将活动的视图设置为其中定义的布局文件。现在,您的活动的父视图是 ID 为“relativeLayout”的相对布局。您需要将 textview 添加到该 relativeLayout 以使其显示。代码如下:

RelativeLayout parent = (RelativeLayout) findViewById(R.id.relativeLayout);

父.addView(textView);

它现在应该出现。

于 2013-08-13T01:05:27.040 回答