마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 스튜디오 한 Activity에 두개의 layout 쓰는 법이 문제인거 같은데...

0 추천
public class MainActivity extends ActionBarActivity {
 
    public static Button taaa;
 
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
 
            final LinearLayout inflated = (LinearLayout)findViewById(R.id.inflated);
 
            Button btn1 = (Button)findViewById(R.id.btn1);
            // 버튼을 누르면 새 뷰가 추가됨.
 
            btn1.setOnClickListener(new Button.OnClickListener(){
                @Override
                public void onClick(View v)
                {
                    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    LinearLayout newBtn = (LinearLayout) inflater.inflate(R.layout.inflated_layout, null);
                    // 추가할 녀석(inflated_layout 이라는 다른 xml 파일에 있다)
                    inflated.addView(newBtn);
                    //inLayout에 inflated_layout을 넣는다.
                }
            });
 
            final LinearLayout newLayout = (LinearLayout)findViewById(R.id.newLayout);
 
            Button btn_del = (Button)findViewById(R.id.btn_del);
 
            btn_del.setOnClickListener(new Button.OnClickListener(){
                @Override
                public void onClick(View v)
                {
                    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    LinearLayout newBtn = (LinearLayout) inflater.inflate(R.layout.inflated_layout, null);
 
                    newLayout.removeViewAt(0);
                }
            });
        }
 
 
 
layout은 activity_main.xml 이랑 inflated_layout.xml 두개인데
파란색은 activity_main.xml에 있는 버튼이고
빨간색은 inflated_layout.xml에 있는 버튼입니다.
 
한 Activity에 두개의 layout을 써서 문제인것은 알겠는데 어떻게 해결해야할지는 잘모르겠습니다.
 
skrxk (140 포인트) 님이 2015년 5월 1일 질문
질문이 뭔지 모르겠습니다. 오류가 나는 증상과 에러메세지를 말씀해 주셔야 할 것 같은데요.

1개의 답변

0 추천
윗분 답변대로 무엇을 하고 싶으신지 모르겠네요;
추가버튼, 삭제버튼을 눌러서 동적으로 버튼을 추가하고, 버튼을 삭제하고 싶으신건가요?

생각하고 계신것처럼 한 Activity에 2개의 layout이 존재하는 것은 구현만 잘 되어 있다면 상관없습니다.

먼저 이렇게 수정해보세요.

1) add버튼과 del 버튼에 각각 동일한 layout을 중복으로 inflate 하고 있네요, 전역변수로 처리하세요.

2) add, del 하고자 하는 button (위의 코드에서는 LinearLayout이네요..)를 굳이 inflate 하는 이유가 있나요?

--> add, del 하고자 하는 button이 customize되어 있는건가요?

3) 그렇지 않다면 activity_main.xml 을 inflate 하시고, 그냥 button 객체를 생성해서 추가/삭제를 하고

그 추가된 버튼들에 대해서 setText로 이름만 변경해주시는 것이 더 나을것 같습니다.
빅클라인 (4,520 포인트) 님이 2015년 5월 4일 답변
...