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

동적으로 생성되는 HorizontalScrollView 관련 질문입니다.

+1 추천

HorizontalScrollView를 통해 ArrayList의 사이즈만큼 버튼을 생성하는 것을 만들고 싶은데요

에러가 나네요...

 

XML>>

            <HorizontalScrollView
                android:id="@+id/category_horizontablscrollview" 
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:fillViewport="true"
                android:scrollbarSize="5dip">
                
                <LinearLayout 
                    android:id="@+id/category_layout"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:orientation="horizontal">
                    
                    <Button 
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"/>
                    
               </LinearLayout>  
                
            </HorizontalScrollView>
 
 
 
Android code>>
 
mScrollView = (HorizontalScrollView) findViewById(R.id.category_horizontablscrollview);
 
        LinearLayout categoryLinearLayout = new LinearLayout(this);
        categoryLinearLayout.setOrientation(LinearLayout.HORIZONTAL); 
 
        for (int i = 0; i < cateList.size(); i++){
            final Button categoryButton = new Button (this);
            categoryButton.setTag(i);
            categoryLinearLayout.addView(categoryButton);
            categoryButton.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Log.e("Tag",""+categoryButton.getTag());
                }
            });
        }
        mScrollView.addView(categoryLinearLayout);
 
 
Error >>
03-17 16:14:22.940: E/AndroidRuntime(8534): Caused by: java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
 

이상입니다..

 

 

 

 

 

---> 자답입니다

View가 한개만 가능하기 때문에

HorizontalScrollView 안을 지워주니 되네요 ㅎㅎ

taejun (7,240 포인트) 님이 2015년 3월 17일 질문
taejun님이 2015년 3월 17일 수정

1개의 답변

0 추천
 
채택된 답변

XML>>

            <HorizontalScrollView
                android:id="@+id/category_horizontablscrollview" 
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:fillViewport="true"
                android:scrollbarSize="5dip">
                
                <LinearLayout 
                    android:id="@+id/category_layout"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:orientation="horizontal">                    
                    
               </LinearLayout>  
                
            </HorizontalScrollView>
 
 
Android code>>
 
mScrollView = (HorizontalScrollView) findViewById(R.id.category_horizontablscrollview);
 
LinearLayout categoryLinearLayout = (LinearLayout)findViewById(R.id.category_layout);
 
        for (int i = 0; i < cateList.size(); i++){
            final Button categoryButton = new Button (this);
            categoryButton.setTag(i);
            categoryLinearLayout.addView(categoryButton);
            categoryButton.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Log.e("Tag",""+categoryButton.getTag());
                }
            });
        }
     
요렇게 하시면됩니다..
mScrollView.addView(categoryLinearLayout); 요건 삭제하시구요..
스크롤뷰 안에 리니어 레이아웃까지 java코드로 생성하실 필요가 없습니다. 
여튼 뷰는 왠만하면 xml로.. button 도 xml파일 따로 만들어서 가져다 쓰시는게 속성 정의 하는데 더 편하고 깔끔할거에요.

 

이드로이드 (22,930 포인트) 님이 2015년 3월 17일 답변
taejun님이 2015년 3월 18일 채택됨
...