2016-05-30 22 views
-1

我mnoob android,我有一個簡單的問題。 我有一個RelativeLayout中的TextView,我想動態地添加一個按鈕,該TextView在RelativeLayout中存在, 我該怎麼做? 謝謝!在Android中使用JAVA類製作按鈕

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:background="@mipmap/background" 
    > 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/container"> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/screen3" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      > 
     </android.support.v4.view.ViewPager> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      > 
     <TextView 
      android:paddingTop="30dp" 
      android:id="@+id/act3_txt1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textSize="35sp" 
      android:textAlignment="center" 
      android:textColor="#FFFFFF" 
      /> 
     <ImageView 
      android:paddingTop="30dp" 
      android:id="@+id/cam_images" 
      android:layout_height="250sp" 
      android:layout_width="wrap_content" 
      android:layout_below="@+id/act3_txt1" /> 
     <TextView 
      android:paddingTop="30dp" 
      android:id="@+id/act3_txt2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/cam_images" 
      android:textSize="35sp" 
      android:textAlignment="center" 
      android:layout_centerInParent="true" 
      android:textColor="#FFFFFF" 
      android:paddingBottom="20dp"/> 

     </RelativeLayout> 
    </RelativeLayout> 


</RelativeLayout> 
+0

下面的按鈕請提供一些源代碼。 –

+0

您的問題顯示零研究和零努力解決您自己的問題,請先嚐試一下,如果您有特定問題,請回來。 – Egor

+0

@Egor請參閱下面的評論我嘗試...我很抱歉,我無法發佈後的代碼。我也是在stackOverFlow noob! –

回答

0

1)在onCreate()中創建一個像這樣的按鈕。

Button myButton = new Button(this); 
myButton.setText("Press Me"); 

2)然後將其添加到您的佈局。在它之前,你必須爲你想要顯示按鈕的佈局添加id。

RelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayoutID); 
layout.addView(myButton); 
0

充氣相對佈局和現有的TextView第一

relativeLayout= (RelativeLayout) findViewById(R.id.relativeLayout); 
textView= (TextView) findViewById(R.id.tittle); 

準備佈局PARAMS和addRule方法把TextView的

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT); 
    params.addRule(RelativeLayout.BELOW, R.id.tittle); 
    Button button=new Button(this); 
    button.setLayoutParams(params); 
    button.setText("Click Me!"); 
    relativeLayout.addView(button,1);