我有两个按钮,想要在同一页面上点击按钮更改布局内容按钮位于顶部。
我想通过单击按钮更改此xml中的特定区域内容。
就像在两页之间切换一样。
提前致谢
它在logcat中显示此异常
**11-15 10:06:02.988: ERROR/AndroidRuntime(261): Uncaught handler: thread main exiting due to uncaught exception
11-15 10:06:03.017: ERROR/AndroidRuntime(261): java.lang.StackOverflowError
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.graphics.Paint.measureText(Paint.java:1020)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.graphics.Paint.measureText(Paint.java:1057)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.text.Styled.foreach(Styled.java:220)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.text.Styled.measureText(Styled.java:371)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.text.Layout.measureText(Layout.java:1601)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.text.Layout.getLineMax(Layout.java:655)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.text.Layout.draw(Layout.java:311)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.text.BoringLayout.draw(BoringLayout.java:356)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.widget.TextView.onDraw(TextView.java:4032)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.widget.CompoundButton.onDraw(CompoundButton.java:229)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.View.draw(View.java:6274)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.drawChild(ViewGroup.java:1526)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.View.draw(View.java:6277)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.widget.FrameLayout.draw(FrameLayout.java:352)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.drawChild(ViewGroup.java:1526)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.View.draw(View.java:6277)
11-15 10:06:03.017: ERROR/AndroidRuntime(261): at android.view.ViewGroup.drawChild(ViewGroup.java:1526)**
答案 0 :(得分:1)
你的布局中需要一个容器 - 比如说LinearLayout
。然后你需要在你的活动中引用它。
LinearLayout container = (LinearLayout) findViewById( R.id.myContainer );
然后您需要为按钮创建onClickListener
:
private class OnButtonClicked implements OnClickListener {
private void onClick( View v ) {
//First we remove what is in the container
container.removeAllViews();
//Then we add the new content
container.addView( newContent );
}
}
newContent
可以是使用LayoutInflater充气的任何视图。
请务必添加OnClickListener
:
myButton.setOnClickListener( new OnButtonClicked() );