将项目添加到子FrameLayout的自定义FrameLayout?

时间:2011-08-10 06:43:10

标签: android

我正在尝试制作某种可以添加内容的按钮,就像对FrameLayout一样。例如:

<ch.pboos.android.ui.ContentButton ...>
  <LinearLayout ...>
    <TextView .../>
    <TextView .../>
  </LinearLayout>
</ch.pboos.android.ui.ContentButton>

到目前为止,我发现了一个xml文件,它代表了我希望按钮看起来如何:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent" android:paddingLeft="@dimen/white_button_margin"
  android:paddingRight="@dimen/white_button_margin">
  <LinearLayout android:layout_width="match_parent"
      android:layout_height="wrap_content" android:orientation="horizontal"
      android:padding="@dimen/white_button_padding"
      android:background="@drawable/channel_bg">

    <FrameLayout android:id="@+id/content" ...>
      <!-- content -->
    </FrameLayout>
    <ImageView ... android:src="@drawable/right_triangle" />
  </LinearLayout>
</LinearLayout>

现在我想创建一个自定义的FrameLayout(ch.pboos.android.ui.ContentButton)来添加该视图,并允许我将视图添加到上面那个xml的内容区域。

有关如何使其正常工作的任何建议吗?

1 个答案:

答案 0 :(得分:1)

1)通过覆盖dispatchTouchEvent将ContentButton上发生的所有触摸事件发送到自定义按钮。并添加单击侦听器到此按钮(这有助于您识别和处理仅点击,而不是例如移动)

2)以这种方式覆盖addView:

addView(...) {
   if(inflatingInProgress) {
      super.addView(view);         
   }
   else {
      // add view to @+id/content
   }
}
// where flag inflatingInProgress initially = true and is set to false after 
// finishing inflating the layout for ContentButton

希望这会有所帮助。祝你好运。

相关问题