2016-05-30 9 views
-1

アンドロイドに私はnoob、私は簡単な質問があります。 私はRelativeLayoutにTextViewを持っていますが、私はRelativeLayoutにあるTextViewのすぐ下にボタンを動的に追加したいのですが、 どうすればいいですか?感謝! ありがとう!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

下のボタンを配置するレイアウトのparamsおよび追加ルール方法を準備し、いくつかのソースコードを提供してください。 –

+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

相対的なレイアウトを膨らませると、最初の

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

既存のTextViewには、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); 
関連する問題