2017-05-30 6 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) { 
      //????????????? 
     } 
    }); 
} 

}

activity_main.appendView(newLayout); 

助けてくれてありがとう...

+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); 

RelativeLayoutからLinearLayoutへactivity_mainのレイアウト(Virticle)

+0

ありがとう、偉大な1つ... – rexo

+0

あなたが感謝のためにupvoteで答えを受け入れるといいでしょう:) – Amy

+0

はい、確かに:)... – rexo

関連する問題