Android的:如何以编程方式添加按钮来查看 [英] Android: How to programmatically add button to view

查看:161
本文介绍了Android的:如何以编程方式添加按钮来查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid活动中,我创建一个继承SurfaceView(使用MonoDroid在语法,这样轻微的变化)的自定义视图:

In my Android activity, I create a custom view that extends SurfaceView (using MonoDroid so slight variations in syntax):

class FriendsView : SurfaceView
{
    ...

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

        ... create my custom view ...

    }


}

在我的活动课,我设置的内容视图到视图:

In my Activity class, I set the content view to the 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中的观点,但我不明白如何使用此声明一个按钮,这是在我看来可见。同样,我找不到在任何活动或视图类,它可以让我增加一个按钮对象编程的方法。

I want to add a button to the view, but cannot work out how to do this. Everything I have read starts from the point of view of the main.xml, but I don't see how to use this to declare a button which is visible in my view. Equally, I cannot find a method in either Activity or View classes which lets me add a Button object programatically.

我确信我失去了一些东西概念,但欢迎任何帮助,让我朝着正确的方向发展。

I feel sure I am missing something conceptually, but would welcome any help to get me going in the right direction.

推荐答案

如果我理解正确的话,你看到你的friendsView就好了,但你要添加一个按钮来呢? 我不知道是什么样的观点你FriendsView是的,但假设它是你可以增加孩子的到(如LinearLayout中或某事),你应该能够做到这一点(只是从我的头顶)

If I understand correctly you see your friendsView just fine, but you want to add a button to it? I don't know what kind of View your FriendsView is, but assuming it is something you can add childs to (like a linearLayout or something), you should be able to do this (just from the top of my head)

 //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()(添加IDS到您的元素,如果你想一些其他的元素找他们这样的)

If there is some other element in friendsView you want to add a child to, you can just find it using findViewById() (add ids to your elements if you want to find them like this)

这篇关于Android的:如何以编程方式添加按钮来查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆