单击按钮创建Imagebutton的新实例

时间:2014-06-16 02:20:12

标签: java android mobile dynamic imagebutton

我编程的应用程序有一个菜单,用户可以在其中选择要构建的新建筑物。用户将按“购买”我希望这样创建一个与该特定建筑相对应的图像按钮的新实例。我目前的问题是,我只是通过在xml文件中设置这些并将它们设置为隐藏直到他们购买它才能理解如何执行此操作。我想在购买时动态创建这些建筑物。有没有办法以这种方式动态创建图像按钮的实例?我真的不认为在这里包含我的源代码是必要的,因为我只需要一个有效的例子(我见过的大多数例子都没有用)。但是如果你想看到一些源代码让我知道。

感谢任何帮助。感谢。

购买建筑按钮内的代码:

newColonyHut = new ImageButton(runGraphics.this);
newColonyHut.setImageResource(R.drawable.mainhut);
newColonyHut.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
layout.addView(newColonyHut);

这是xml:

    <LinearLayout
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <RelativeLayout
            android:id="@+id/layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" 
            android:layout_marginLeft="0dp"
            android:layout_marginRight="20dp" >

            <Button
                android:id="@+id/buyColonyHut"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="5dp"
                android:layout_below="@+id/colonyHutPrice" />
    </LinearLayout>

1 个答案:

答案 0 :(得分:1)

您可以创建ImageView并动态添加到您指定的布局。

<强>样品:

oncreate()

中的

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Button b = (Button) findViewById(R.id.button1);
    final LinearLayout test = (LinearLayout)findViewById(R.id.test);

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ImageView image = new ImageView(MainActivity.this);
            image.setImageResource(your_image_here);
            image.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
            test.addView(image);
        }
    });

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" >

<LinearLayout
    android:id="@+id/test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/layer"
        android:text="Button" />
</LinearLayout>

如您所见,在布局中单击按钮时会创建ImageView,然后动态地将其添加到LinearLayout

LayoutParams用于设置ImageView尊重其父级LinearLayout

的高度和宽度