【发布时间】:2012-06-10 02:46:07
【问题描述】:
我这里有一个自定义视图,然后我想要一些小部件来配合它。
<com.zone.manager.Tab3
android:id="@+id/tab3_display"
android:layout_width="fill_parent"
android:layout_height="620dp" >
<Button
android:id="@+id/addZone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Zone" />
</com.zone.manager.Tab3>
现在我想将该按钮设置为 onclicklistener,这样我就可以在 View 类中使用它做一些事情,所以我这样做了...
addZone = (Button) findViewById(R.id.addZone);
addZone.setOnClickListener(this);
我在
public class Tab3 extends ViewGroup implements OnTouchListener, OnClickListener
Public Tab3(Context context, AttributeSet attrs)
{
super (context, attrs);
// here
}
当我扩展 ViewGroup 时,它让我实现了这个
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
// TODO Auto-generated method stub
}
为了完成这项工作,我应该在此处放入一些东西吗?
但是当我尝试运行应用程序时它崩溃了,但是如果我 // 退出 addZone.setOnClickListener(this); 应用程序运行正常,对我有什么帮助吗?
标签
th.setup();
TabSpec specs = th.newTabSpec("tag0");
specs.setContent(R.id.connecttionTab);
specs.setIndicator("Connection Tab");
th.addTab(specs);
specs = th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("Zone Manager");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("",res.getDrawable(R.drawable.ic_tab_vaccontrol));
th.addTab(specs);
//this is the tab that has all this vvv
specs = th.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("Graphical Layout");
th.addTab(specs);
【问题讨论】:
-
当您更改为 ViewGroup 时,您是否也更改了 xml 以使 Button 是嵌套在 Tab3 元素内的子元素?如果它只是像您发布的 xml 一样跟随它,
findViewById仍然找不到它。而且,是的,您需要实现onLayout来决定每个子元素(在本例中,只是 Button)应放置在父 Tab3 元素的边界内的位置。 -
对不起,我不明白这是什么意思。你能给我解释一下吗?
-
在我的回答中查看我的第一个示例 xml 代码。请注意,第一个标签 not 结束
Tab3元素。 (它以>而不是/>结尾,并且有一个</com.zone.manager.Tag3>标签在Button标签之后。)Button元素是Tab3. -
我建议您使用我建议的第二种布局方法。我不知道您到底想要什么布局,所以我无法提供具体的布局供您尝试。你能解释一下屏幕上的布局应该是什么(即 Tab3 应该显示在哪里以及按钮应该显示在哪里)?如果按钮应该在 Tab3 的顶部,那么您也许可以使用 RelativeLayout 或 FrameLayout 来获得良好的效果。
-
该按钮应该位于 Tab3 下方,您能否编辑您的答案以向我展示我想要做什么,因为我很困惑。
标签: android class view android-widget