2017-05-30 2 views
0

Ich bin neu in Android-Programmierung, ich erstellte add_layout.xml und was ich will, ist es in activity_main.xml untereinander durch OnClick jedes Mal, wenn die Schaltfläche geklickt hinzufügen. hier ist die Codes: add_layout:setOnClickListener zum Erstellen des gleichen Layouts untereinander

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="60dp" 
android:id="@+id/addRoot"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:text="Hello World..." 
    android:id="@+id/plainText"/> 

<View 
    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:background="#000000" 
    android:layout_below="@id/plainText" 
    android:layout_marginTop="16dp"/> 

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
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" 
tools:context="com.app.androidstudio.addlayout.MainActivity"> 

<Button 
    android:layout_width="150dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/add" 
    android:text="Add" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true"/> 

<Button 
    android:layout_width="150dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/addAnother" 
    android:text="Add another" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="false" /> 

MainActivity.java:

public class MainActivity extends AppCompatActivity { 
Button add, addAnother; 
RelativeLayout activity_main; 
RelativeLayout add_layout; 
TextView plainText; 
boolean clickAgain; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    add = (Button) findViewById(R.id.add); 
    addAnother = (Button) findViewById(R.id.addAnother); 
    plainText = (TextView) findViewById(R.id.plainText); 

    add_layout = (RelativeLayout) findViewById(R.id.addRoot); 


    activity_main = (RelativeLayout) findViewById(R.id.activity_main); 

    add.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      final RelativeLayout newLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.add_layout, null); 
      activity_main.addView(newLayout); 

     } 
    }); 

    addAnother.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //????????????? 
     } 
    }); 
} 

}

Dank für die Hilfe ...

+1

Welches Problem Sie konfrontiert sind? – Amy

+0

@Amy jedes Mal, wenn ich auf Hinzufügen, das Layout auf einander, nicht unter. – rexo

+0

Ich möchte sie untereinander – rexo

Antwort

0
ändern

Ändern Sie das relativeLayout von Activity_M ain.xml Mit LinearLayout mit vertikaler Ausrichtung.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
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" 
orientation="vertical" 
tools:context="com.app.androidstudio.addlayout.MainActivity"> 

<Button 
    android:layout_width="150dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/add" 
    android:text="Add" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true"/> 

<Button 
    android:layout_width="150dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/addAnother" 
    android:text="Add another" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="false" /> 
</LinearLayout> 

und Ihre MainActivity

public class MainActivity extends AppCompatActivity { 
Button add, addAnother; 
LinearLayout activity_main; 
RelativeLayout add_layout; 
TextView plainText; 
boolean clickAgain; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    add = (Button) findViewById(R.id.add); 
    addAnother = (Button) findViewById(R.id.addAnother); 
    plainText = (TextView) findViewById(R.id.plainText); 

    add_layout = (RelativeLayout) findViewById(R.id.addRoot); 


    activity_main = (LinearLayout) findViewById(R.id.activity_main); 

    add.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      final RelativeLayout newLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.add_layout, null); 
      activity_main.addView(newLayout); 

     } 
    }); 

    addAnother.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //????????????? 
     } 
    }); 
} 
+0

Einfache und einfache Antwort, Danke. Es funktioniert ... – rexo

+0

danke, wir sind hier, um Ihnen zu helfen. – MageNative

1

ändern Diese

activity_main.addView(newLayout); 

Um

activity_main.appendView(newLayout); 

Und auch das Layout von activity_main von RelativeLayout zu LinearLayout (Virticle)

+0

Danke, tolle Eins ... – rexo

+0

wäre es gut, wenn Sie die Antwort mit dem upvote für Wertschätzung akzeptieren :) – Amy

+0

Ja, sicher:) ... – rexo

Verwandte Themen