Java 通过单击按钮创建Imagebutton的新实例
我正在编程的应用程序有一个菜单,用户可以在其中选择要建造的新建筑。用户将按下“购买”,我希望它创建一个与该特定建筑对应的imagebutton的新实例。我目前的问题是,我只知道如何在xml文件中设置这些,并在他们购买之前将它们设置为不可见。我想动态创建这些建筑物时,他们被购买。有没有一种方法可以动态创建一个图像按钮的实例?我真的不认为在这里包含我的源代码是必要的,因为我只需要一个有效的示例(我看到的大多数示例都不起作用)。但是如果你想看一些源代码,请告诉我 感谢您的帮助。谢谢 “购买建筑物”按钮内的代码:Java 通过单击按钮创建Imagebutton的新实例,java,android,mobile,dynamic,imagebutton,Java,Android,Mobile,Dynamic,Imagebutton,我正在编程的应用程序有一个菜单,用户可以在其中选择要建造的新建筑。用户将按下“购买”,我希望它创建一个与该特定建筑对应的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>
您可以创建
ImageView
并动态添加到指定的布局中
示例:
在oncreate()中
activity\u 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
LayoutParams
是设置ImageView
的高度和宽度的地方,它的父视图是LinearLayout
,我将给它一个镜头,并用我的结果进行更新。谢谢。当我尝试你展示的设置时,我得到一个nullpointerexception,它指向你的测线@saboehnke在您的activity_main.xml中是否有一个包含android:id=“@+id/test”的线性布局?是的。问题是按钮位于线性布局内部的相对布局内部。所以我假设我需要对布局参数使用相对布局,对吗?@saboehnke布局字段是什么?
<?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>