2014-10-20 74 views
1

你好,我需要参加4个独立的图像转换成一个ImageButton如何加入4个图像一个图像按钮(机器人)

我要附加应该是哪一个像上一个图像按钮,为感谢您的帮助图像。 我该怎么办?

Img1 on top 
Img2 img3 at the middle 
Img4 at bottom 

像一个金字塔

+0

的4你试试?并显示你想要的图像 – MilapTank 2014-10-20 08:47:38

+0

你将不得不使用画布 – 2014-10-20 08:49:17

回答

1

你可以用按钮做到这一点:

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:drawableTop="@drawable/btn_back" 
    android:drawableLeft="@drawable/bd_logo" 
    android:drawableRight="@drawable/btn_close" 
    android:drawableBottom="@drawable/btn_ok"/> 

左图:

yourButtonId.setCompoundDrawablesWithIntrinsicBounds(R.drawable.smiley, 0, 0, 0); 
+1

他希望img2和img3在中间。你不能用这个xml设置它们 – 2014-10-20 08:49:58

+0

我可以从Java代码中更改顶部/左侧/右侧/下部吗? – 2014-10-20 09:03:53

1

其实你不必使用imageButton为了那个原因。您可以创建一个新的LinearLayout,并将setOnClickListener方法分配给该LinearLayout。所以它的行为就像一个按钮,你可以按照你想要的方式找到许多图像。例如;

LinearLayout的XML代码;

<LinearLayout 
    android:id="@+id/imagebutton" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/imageView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_launcher" /> 

     <ImageView 
      android:id="@+id/imageView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

    <ImageView 
     android:id="@+id/imageView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" /> 
</LinearLayout> 

您的活动中的声明;

LinearLayout imageButton = (LinearLayout) findViewById(R.id.imagebutton); 

处理点击事件;

imageButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      //TODO 

     } 
    }); 
+0

你的想法很酷,但问题是这是一个棋盘游戏,“计算机”必须迭代图像按钮矩阵。我可以在这些布局上循环使用for循环吗? – 2014-10-20 09:00:41