[英]Adding a Textview to a relative layout in Android
因此,我到處都進行了研究,但似乎沒有其他問題似乎是因為他們知道如何編程,但是無論如何我都試圖將TextView添加到我的Android應用程序中的相對布局中,但是無論如何我都無法獲得文字和我已經在布局中的按鈕。 這是我的活動代碼:
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>
您已經有了RelativeLayout
所以您不需要做
RelativeLayout marble= new RelativeLayout(this);
這將創建一個新的RelativeLayout
。 只需創建你TextView
並將其添加到這個layout
你后inflate
與它setContentView()
public class EnterApp extends Activity {
int marblesInJar=0;
int targetMarblesInJar=0;
RelativeLayout marble;
@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);
在onCreate()方法中,您具有setContentView()方法,該方法將活動的視圖設置為其中定義的布局文件。 現在,您的活動的父視圖是ID為“ relativeLayout”的相對布局。 您需要將textview添加到該relativeLayout才能顯示。 其代碼如下:
RelativeLayout parent =(RelativeLayout)findViewById(R.id.relativeLayout);
parent.addView(textView);
它應該現在顯示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.