我是android编程的新手,我创建了add_layout.xml,我想要的是每次按下按钮时都通过onClick将它添加到activity_main.xml中。 这是代码: add_layout:
<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) {
//?????????????
}
});
}
}
感谢您的帮助...
答案 0 :(得分:1)
更改此
activity_main.addView(newLayout);
到
activity_main.appendView(newLayout);
并将activity_main的布局从RelativeLayout
更改为LinearLayout
(Virticle)
答案 1 :(得分:0)
使用LinearLayout更改Activity_Main.xml的RelativeLayout,方向为vertical。
<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) {
//?????????????
}
});
}