1

这是交易。

我正在查看有关扩展 ImageView 的代码:

http://marakana.com/forums/android/examples/98.html

我想知道如何将新视图与其他一些视图一起添加到现有的 xml 布局文件中。

我已经在我的主要线性布局中这样做了:

<FrameLayout android:id="@+id/FrameLayout01" android:layout_width="300dp" android:layout_height="300dp">
        <com.marakana.Rose android:layout_height="wrap_content" android:layout_width="wrap_content"/>

但问题是这样就不会调用 onDraw 方法。

任何人都可以为此提出解决方案吗?也许是一些将 CustomViews 与 xml 布局结合起来的例子。

tnx。

4

3 回答 3

2

您是否在res/values/attrs.xml添加了定义?

<declare-styleable name="Rose">
</declare-styleable>

如果您不添加任何 xml 属性,我不确定是否需要它...

此外,如果您发布 com.marakana.Rose 代码也会有所帮助。举个例子,这是我自己的一个组件的类声明:

public class CalendarCell extends ImageButton implements OnTouchListener

绘制:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

这是我的 xml 布局的片段:

<TableRow>
        <com.view.customcomponents.CalendarCell
            style="@style/NumericImageButton"
            android:id="@+id/calendar_row1_col1"

            android:background="@drawable/calendar_bg_selector" />

前段时间我按照教程做了这个,但我不记得在哪里。另外,我可能会遗漏一些东西。谷歌一点,你会发现有用的资源。

编辑:我认为这些是我遵循的教程,尽管我必须付出一些努力才能最终使它起作用:

官方文档

AndDev.org

于 2010-09-15T09:21:21.230 回答
0

我想我解决了。我在 main.xml 文件中使用了我的代码,它可以工作。我必须做的是重写一个新的类构造函数,它接受 AttributesSet 以及上下文作为参数,然后使用 findViewById(CustomViewName) 在活动中引用它并使用在 CustomView 中定义的函数。

于 2010-09-15T23:46:19.130 回答
0

要允许 Android 开发者工具与您的视图交互,您至少必须提供一个构造函数,该构造函数将Context 和 AttributeSet对象作为参数。此构造函数允许layout editor to create and edit an instance of your view.

class PieChart extends View {
    public PieChart(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}

并在此处详细回答

于 2015-05-25T08:23:41.640 回答