2017-01-01 11 views
-2

ボタンをクリックすると、Androidスタジオのアクティビティのレイアウトを変更する際に助けが必要です。Androidスタジオのアクティビティレイアウトの変更

しかし、私は新しいアクティビティを開きたくありません。ボタンをクリックして、現在のアクティビティでレイアウトとデザインを完全に変更したいと思います。出来ますか ?

答えて

-1

はい、setContentView()メソッドを使用すると可能です。ただし、現在のレイアウトでは利用できないビュー(IDによるビューなど)を使用していないことを確認する必要があります。

0

あなたはbuttonクリック時に別のレイアウトを膨らませることができます:XMLを膨らませる

public class LayoutInflateDemo extends Activity { 

    private LinearLayout layoutToAdd; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout_existed); 
     layoutToAdd = (LinearLayout) findViewById(R.id.existedlayout); 

     Button button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       LayoutInflater inflater = LayoutInflater 
         .from(getApplicationContext()); 
       View view = inflater.inflate(R.layout.layout_toinfliate, null); 
       //Add the inflated layout to your existing view 
       layoutToAdd.addView(view); 

      } 
     }); 
    } 
} 

レイアウト:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
    android:id="@+id/mytext" 
    android:text="Inflated layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout> 

そして、これは、既存のレイアウトです:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/existedlayout" 
    > 
    <Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Clicck to inflate view" 
    android:id="@+id/button" 
    /> 
</LinearLayout> 
+0

単一のアクティビティに複数のボタンがある場合、レイアウトを切り替えるにはどうすればよいですか? – user3102147

+0

複数のボタンをクリックしているときに違うレイアウトを膨張させ、既存のレイアウトに追加します – rafsanahmad007

+0

私はアンドロイドスタジオの新作です。愚かな質問を申し訳ありません。しかし、私はXmlを作成するか、すでにそこにありますか?このコードを追加しますか? – user3102147

関連する問題