动态添加按钮到 Fingerpaint Android api 演示

发布于 2024-11-06 06:59:09 字数 97 浏览 0 评论 0原文

现在我想在 FingerPaint api 演示中动态添加按钮。但问题是我不熟悉在java文件中动态创建布局。有人知道我如何实现此类以在标题栏中添加按钮吗?任何代码示例将不胜感激。

Now i would like to add button dynamically in FingerPaint api demos. But the problem is that i ain't familiar with creating layout dynamically in java file. does somebody know how can i implement this class to add buttons in title bar? Any code samples would be appreciate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巡山小妖精 2024-11-13 06:59:09

说明:

1) 获取对插入动态 gui 组件的布局容器的引用。如果容器是动态创建的,那么您已经拥有对它的引用。如果它来自 xml 布局,您可以使用 findViewById 获取引用。

2) 创建动态组件。您需要将上下文传递给构造函数:使用 this

3) 设置创建的组件属性。

4) 使用container.addView(component)将组件动态添加到容器中。


分步演示:

1) 使用项目助手创建一个新的 Android 项目,其中包含默认选项、包 test.testMain 活动。

2) 按如下方式编辑 res/layout/main.xml 文件。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <LinearLayout 
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/layoutId"
            >
        </LinearLayout>
    </LinearLayout>

3) 按如下方式编辑src/test.test/Main.java文件。

    package test.test;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.Toast;

    public class Main extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            addDynamicButton();
        }

        /**
         * Adds a dynamic button.
         */
        private void addDynamicButton() {
            // creates a button dynamically
            Button btn = new Button(this);
            // sets button properties
            btn.setText("I'm dynamic. Please click me.");
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast toast = Toast.makeText(Main.this, "Yo!", Toast.LENGTH_LONG);
                    toast.show();
                }
            });
            // retrieve a reference to the container layout
            LinearLayout container = (LinearLayout)findViewById(R.id.layoutId);
            // adds dynamic button to the GUI
            container.addView(btn);
        }
    }

4) 编译并作为Android应用程序运行。现在您知道它是如何工作的,并且可以使用此技术将任何类型的组件添加到任何布局容器。

Explanation:

1) Get a reference to the layout container where to insert the dynamic gui component. If the container was created dynamically, you already have a reference to it. If it comes from a xml layout, you can get a reference with findViewById.

2) Create a dynamic component. You need to pass a context to the constructor: Use this.

3) Set the created component properties.

4) Use container.addView(component) to add the component dynamically to the container.


Step by step demo:

1) Use the project assistant to create a new Android project with default options, package test.test, and a Main activity.

2) Edit the res/layout/main.xml file as follows.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <LinearLayout 
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/layoutId"
            >
        </LinearLayout>
    </LinearLayout>

3) Edit the src/test.test/Main.javafile as follows.

    package test.test;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.Toast;

    public class Main extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            addDynamicButton();
        }

        /**
         * Adds a dynamic button.
         */
        private void addDynamicButton() {
            // creates a button dynamically
            Button btn = new Button(this);
            // sets button properties
            btn.setText("I'm dynamic. Please click me.");
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast toast = Toast.makeText(Main.this, "Yo!", Toast.LENGTH_LONG);
                    toast.show();
                }
            });
            // retrieve a reference to the container layout
            LinearLayout container = (LinearLayout)findViewById(R.id.layoutId);
            // adds dynamic button to the GUI
            container.addView(btn);
        }
    }

4) Compile and run as an Android application. Now you know how it works and you can use this technique to add any kind of component to any layout container.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文