这是交易。

我正在查看有关扩展图像视图的此代码:

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。

有帮助吗?

解决方案 2

我想我解决了。我在main.xml文件中使用了我的代码,并且可以使用。我要做的是覆盖一个新的类构造函数,将属性与上下文一起作为参数,然后使用findViewById(customViewName)将其引用在活动中,并使用CustomView中定义的函数。

其他提示

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

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

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

另外,如果您发布了com.marakana.rose代码,这将有所帮助。例如,这是我自己的一部分的类声明:

public class CalendarCell extends ImageButton implements OnTouchListener

on Draw:

@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" />

一段时间以前,我遵循教程,但我不记得在哪里。另外,我可能会缺少一些东西。 Google有点谷歌,您会发现有用的资源。

编辑:我认为这些是我遵循的教程,尽管我不得不投入一些工作才能使其最终起作用:

官方文档

anddev.org

为了允许Android开发人员工具与您的观点进行交互,至少必须提供一个构造函数 上下文和属性 对象为参数。该构造函数允许 layout editor to create and edit an instance of your view.

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

和细节 在这里回答

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top