所以我到处研究过,但似乎没有其他人有这个问题,可能是因为他们知道如何编程,但无论如何我试图在我的 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>