2016-05-30 12 views
-1

Ich m noob zu Android, ich habe eine einfache Frage. Ich habe TextView in einem RelativeLayout, ich möchte dynamisch unterhalb der TextView eine Schaltfläche hinzufügen, die in einem RelativeLayout, vorhanden ist, wie kann ich das tun? danke!machen Knöpfe in android mit JAVA-Klassen

<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

bitte etwas Quellcode bereitstellen zu setzen. –

+0

Ihre Frage zeigt null Forschung und Null Anstrengung, um das Problem auf eigene Faust zu lösen, bitte versuchen Sie dies zuerst und kommen Sie zurück, wenn Sie ein bestimmtes Problem haben. – Egor

+0

@Egor Bitte beachten Sie den Kommentar unten Ich versuche ... Es tut mir leid, ich konnte den Code in der Post nicht veröffentlichen. Ich bin auch auf StackOverFlow! –

Antwort

0

1) Erstellen Sie eine Schaltfläche wie diese in onCreate().

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

2) dann fügen Sie es in Ihr Layout ein. davor müssen Sie ID für Ihr Layout hinzufügen, in dem Sie die Schaltfläche anzeigen möchten.

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

aufpumpen die relative Anordnung und die bestehende Textview erste

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

Layout params und addrule Methode Bereiten Sie die Taste unterhalb des 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); 
Verwandte Themen