2013-11-19 4 views
0

많은 관련 문제가 있다는 것을 알고 있지만 구체적인 대답을 찾고 있습니다. 아래의 코드는 앱에서 주로 사용하는 XML입니다. 이제 내 질문. 수평 뷰의 내용을 동적으로 변경할 수 있어야합니다. 여기에는 Views를 삭제하고 새로운보기를 추가하는 작업이 포함됩니다. 선형 레이아웃에 ID를 할당하려 할 때 유형 문자열 ID를 수락 할 수 없다는 오류가 발생하면서 정확히 어떻게이 작업을 수행 할 것인가?가로 스크롤 한 어린이보기 (Eclipse IDE, Android)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     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=".MainActivity" > 

     <TextView 
      android:id="@+id/txtClassification" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:text="Classification:&lt;>" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      tools:ignore="HardcodedText" /> 

     <Spinner 
      android:id="@+id/spinGoto" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:layout_marginBottom="56dp" /> 

     <Button 
      android:id="@+id/btnBack" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:text="Go Back" 
      tools:ignore="HardcodedText" /> 

     <Button 
      android:id="@+id/btnSelect" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:text="Select" 
      tools:ignore="HardcodedText" /> 

     <HorizontalScrollView 
      android:id="@+id/gallery" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/txtClassification" 
      android:layout_alignRight="@+id/spinGoto" 
      android:layout_below="@+id/txtClassification" > 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="horizontal" > 

      </LinearLayout> 
     </HorizontalScrollView> 

    <ImageView 
     android:id="@+id/myImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

    </RelativeLayout> 

모든 도움을 주실 수 있습니다. HorizontalScrollView는 하나의 직접 아이를 가질 수 있기 때문에

답변

0

당신은 LinearLayout 작업을해야합니다.

<HorizontalScrollView 
     android:id="@+id/gallery" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/txtClassification" > 

     <LinearLayout 
      android:id="@+id/yourLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

     </LinearLayout> 
    </HorizontalScrollView> 

그런 다음 당신은 프로그래밍이

0

ID입니다은 predifined 형식을 사용하는 데 도움이보기

public void addView(View v) { 
    LinearLayout yourLayout = (LinearLayout) findViewById(R.id.yourLayout); 
    yourLayout.addView(v); 
} 

public void clearLayout() { 
    LinearLayout yourLayout = (LinearLayout) findViewById(R.id.yourLayout); 
    yourLayout.removeAllViews(); 
} 

public void removeView(View v) { 
    LinearLayout yourLayout = (LinearLayout) findViewById(R.id.yourLayout); 
    yourLayout.removeView(v); 
} 

희망을 추가하거나 제거 할 수 액세스 할 수 있습니다. "@+id/" 구문 뒤에 원하는 이름을 입력하십시오. 예 android:id="@+id/layout".

그런 다음 코드에서 HorizontalScrollView의 LinearLayoutinside을 얻을 :

LinearLayout layout = findViewById(R.id.layout); 

이 레이아웃의 같이 adView() 전화 레이아웃에 뷰를 추가하려면. 예 :

//add 5 buttons 
for (int i = 1; i <= 5 ; i++){ 
Button btn = new Button(this); 
btn.setId(i); 
btn.setText(i+""); 
layout.addView(btn); 
} 

레이아웃 호출에서 removeView()을보기를 제거하려면

//remove button nr. 3 
Button btn3 = layout.findViewById(3); 
layout.removeView(btn3); 

HorizontalScrollView 방금 채우고 중첩있는 LinearLayout 내부의 요소를 구성 할 수 있고, 모든 것을 처리합니다.

행운을 빈다.