나는 그것을 알아 냈지만 명확한 해결책을 얻지 못했기 때문에 내 질문에 대답하고 있습니다. 이것은 다른 사람을 도울 수 있습니다.
받은 예외가 일반적인 예외라는 것을 알고 있지만 다음 페이지에서 문제를 해결할 수 없습니다.
지정된 하위에 이미 상위가 있습니다. 자식의 부모 첫 번째
java.lang.IllegalStateException 에서 removeView ()를 호출해야합니다 . 지정된 자식은 이미
자식의 부모에서 부모 호출 removeView ()를 가지고 있습니다.
http://www.phonesdevelopers.com/1716777/
내가하고 싶은 것은에 많은 TextView
s를 추가 한 ScrollView
다음 ScrollView
화면에 넣습니다 . 문제는이 ScrollView
화면의 일부일 뿐이 setContentView(scrollView)
므로 이미 가지고있는 버튼과 물건을 대체 할 수 있기 때문에 사용할 수 없다는 것입니다.
내 레이아웃에 대한 템플릿이 포함 된 XML 파일이 있는데을 ScrollView
내 활동에서 생성 한 파일로 간단히 바꾸고 싶습니다 .
레이아웃은 다음과 같습니다.
<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:id="@+id/mostParent" >
<RelativeLayout
android:id="@+id/loadingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
</RelativeLayout>
<Button
android:id="@+id/btnLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/logout" />
<ImageButton
android:id="@+id/refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_action_refresh" />
<ToggleButton
android:id="@+id/lockToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text=""
android:textOff="@string/toggle_off"
android:textOn="@string/toggle_on" />
<TextView
android:id="@+id/loggedInAs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/refresh"
android:layout_centerHorizontal="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<ScrollView
android:id="@+id/postsView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/btnLogout"
android:layout_marginTop="30dp"
android:scrollbarSize="3dip" />
</RelativeLayout>
ScrollView
하단에 내가 추가 할 하나 TextView
에들 그리고 ID를 가지고 postsView
.
나는를 만들거야 그래서 LinearLayout
, 추가 TextView
그 레이아웃의를 한 다음에 그 레이아웃을 추가합니다 ScrollView
. 이렇게 :
ScrollView postsView = (ScrollView)findViewById(R.id.postsView);
LinearLayout layout = new LinearLayout(MyActivity.this);
layout.setOrientation(LinearLayout.VERTICAL);
for (final TextView post : posts) {
// add post to layout
layout.addView(post);
}
postsView.addView(layout);
그러나 이것으로 LogCat을 얻습니다.
java.lang.IllegalStateException: ScrollView can host only one direct child
에서보기를 제거하려면 어떻게해야 ScrollView
합니까?
당신이 사용할 수있는 removeAllViews()
온 ScrollView
. 이는 코드가 두 번 이상 실행될 때마다 둘 이상의 뷰가 ScrollView
.
코드는 다음을 변경합니다.
postsView.addView(layout);
에:
postsView.removeAllViews()
postsView.addView(layout);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다