在我的android活动中,我创建了一个扩展surfaceview的自定义视图(使用monodroid,语法上的细微变化):

class FriendsView : SurfaceView
{
    ...

    public FriendsView(Context context) : base(context)
    {

        ... create my custom view ...

    }


}

在活动类中,我将内容视图设置为视图:
protected override void OnCreate(Bundle bundle)
{

    base.OnCreate(bundle);

    ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.FillParent);

    FriendsView friendsView = new FriendsView(this);

    SetContentView(friendsView, layoutParams);

}

我想在视图中添加一个按钮,但无法确定如何执行此操作。我所读的所有内容都是从main.xml的角度开始的,但是我不知道如何使用它来声明在我的视图中可见的按钮。同样,我在activity或view类中都找不到允许我以编程方式添加button对象的方法。
我确信我在概念上漏掉了一些东西,但是我欢迎任何帮助,让我朝着正确的方向前进。

最佳答案

如果我理解正确的话,你的朋友视图很好,但是你想添加一个按钮吗?
我不知道你的朋友视图是什么样的,但是假设你可以给它添加childs(比如linearlayout之类的),你应该可以这样做(只是从我的头顶)

 //assuming you have a friendsView object that is some sort of Layout.
 Button yourButton = new ImageButton(this);
 //do stuff like add text and listeners.

 friendsView.addView(yourButton);

如果friendsview中有其他元素要添加子元素,可以使用findViewById()找到它(如果您想这样找到它们,可以向元素添加id)

07-24 09:47
查看更多