2017-05-30 34 views
0

我是一个新的android编程,我创建了add_layout.xml,我想要的是每次点击按钮时通过onClick将它添加到activity_main.xml中。 这里是代码: add_layout:setOnClickListener创建相同的布局下彼此

<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) { 
      //????????????? 
     } 
    }); 
} 

}

感谢您的帮助......

+1

你正面临什么问题? – Amy

+0

@Amy每次点击添加,布局相互添加,而不是下面。 – rexo

+0

我想让它们在对方下面 – rexo

回答

0

更改Activity_M的RelativeLayout ain.xml使用垂直方向的LinearLayout。

<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> 

和您的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

简单和容易的答案,谢谢。它的工作原理... – rexo

+0

谢谢你,我们在这里帮助你。 – MageNative

1

改变这种

activity_main.addView(newLayout); 

activity_main.appendView(newLayout); 

而且还从RelativeLayout改变activity_main的布局LinearLayout(Virticle)

+0

谢谢,很好的一个... – rexo

+0

这将是一件好事,如果你接受upvote为赞赏的答案:) – Amy

+0

是的,当然:))... – rexo