答案 0 :(得分:3)
正式解决方案
在roundedshape.xml
drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:innerRadiusRatio="4"
android:shape="ring"
android:thicknessRatio="5"
android:useLevel="false">
<solid android:color="#af08d7" />
<size
android:width="25dip"
android:height="25dip"></size>
</shape>
</item>
</layer-list>
添加RelativeLayout
1>}
android:background="@drawable/roundedshape"
在<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="300dp"
android:padding="50dp"
android:background="@drawable/roundedshape"
android:id="@+id/myRelativeLayout"
>
</RelativeLayout>
添加这些代码以动态生成Activity
并添加ImageButtons
。
Layout
在 RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout);
for (int j = 0; j < 4; j++) {
ImageButton myImageButton = new ImageButton(this); //generate ImageButton
myImageButton.setId(j); //Set Id of button
//Generate Random Number to place ImageButtons in random position
int min = 40;
int max = 60;
Random r = new Random();
int randomNum = r.nextInt(max - min + 1) + min;
myImageButton.setBackgroundResource(R.drawable.chatbox); //Set background of ImageButton
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
params.leftMargin = randomNum+j*randomNum; //Generate left margin dynamically
params.topMargin = randomNum +j*randomNum; //Generate right margin dynamically
relativeLayout.addView(myImageButton, params); //Add view
myImageButton.setOnClickListener(getButtonAndDoAction(myImageButton)); //Add OnClickListener
}
中添加此方法以处理图片按钮的点击事件。
Activity
结果:
注意: 我添加了评论以尽可能清除代码
答案 1 :(得分:0)
您可以将图像绘制为:
RelativeLayout
这取决于你的目标,也许你需要自定义布局,也许你的游戏中有一些精灵或者位图可以保存到文件系统中。