私は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) {
//?????????????
}
});
}
}
activity_main.appendView(newLayout);
へ
助けてくれてありがとう...
あなたが直面している問題は何? – Amy
@Amy私が追加をクリックするたびに、レイアウトはお互いに追加されました。 – rexo
私はそれらをお互いの下に置いてほしい – rexo