0

我有两个图像按钮,button1 和 button2。对于按钮 1,我正在设置一个更大的图像,而对于按钮 2,我正在设置一个小图像。我必须将 button2 准确地放在 button1 的中心。我如何实现这一目标?

我正在使用相对布局,我必须在XML文件中进行此更改。我还需要分别为这两个按钮接收点击事件。

我已经尝试了下面的代码,但我没有收到单独的点击事件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/linearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/mainscreen"
        android:scaleType="fitXY" />

    <com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="320dp"
        android:layout_height="50dp"
        ads:adUnitId="xxxx"
        ads:adSize="BANNER"
        ads:loadAdOnCreate="true"/>

    <ImageButton android:id="@+id/star"
        android:layout_width ="wrap_content"
        android:layout_height ="wrap_content"
        android:layout_toRightOf="@+id/adView"
        android:src="@drawable/star"
        android:background="@layout/selector"
        android:layout_marginLeft="40dp"
        android:focusable="false" />

    <ImageButton android:id="@+id/image1"
        android:layout_width ="wrap_content"
        android:layout_height ="wrap_content"
        android:layout_below="@+id/adView"
        android:src="@drawable/drum"
        android:background="@layout/selector"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="10dp"
        android:focusable="false" />
    <ImageButton android:id="@+id/image2"
        android:layout_width ="wrap_content"
        android:layout_height ="wrap_content"
        android:src="@drawable/drum1"
        android:layout_alignLeft="@+id/image1"
        android:layout_alignTop="@+id/image1"
        android:layout_alignRight="@+id/image1"
        android:layout_alignBottom="@+id/image1"
        android:focusable="false" />

    <ImageButton android:id="@+id/image3"
        android:layout_width ="wrap_content"
        android:layout_height ="wrap_content"
        android:src="@drawable/drum2"
        android:background="@layout/selector"
        android:layout_alignLeft="@+id/image1"
        android:layout_alignTop="@+id/image1"
        android:layout_alignRight="@+id/image1"
        android:layout_alignBottom="@+id/image1"
        android:focusable="false" />

    <ImageButton android:id="@+id/image4"
        android:layout_width ="wrap_content"
        android:layout_height ="wrap_content"
        android:layout_below="@+id/adView"
        android:src="@drawable/drum"
        android:background="@layout/selector"
        android:layout_marginTop="30dp"
        android:layout_toRightOf="@+id/image1"
        android:focusable="false" />

    <ImageButton android:id="@+id/image5"
        android:layout_width ="wrap_content"
        android:layout_height ="wrap_content"
        android:src="@drawable/drum1"
        android:background="@layout/selector"
        android:layout_alignLeft="@+id/image4"
        android:layout_alignTop="@+id/image4"
        android:layout_alignRight="@+id/image4"
        android:layout_alignBottom="@+id/image4"
        android:focusable="false" />

    <ImageButton android:id="@+id/image6"
        android:layout_width ="wrap_content"
        android:layout_height ="wrap_content"
        android:src="@drawable/drum2"
        android:background="@layout/selector"
        android:layout_alignLeft="@+id/image4"
        android:layout_alignTop="@+id/image4"
        android:layout_alignRight="@+id/image4"
        android:layout_alignBottom="@+id/image4"
        android:focusable="false" />
</RelativeLayout>
4

4 回答 4

2

使用您当前的布局,您将需要嵌套项目。但由于您只对 imagebutton 的图像部分感兴趣,因此您可以只使用 FrameLayout 作为您当前调用的“button1”,就像在我的示例中一样(适应您的情况):

<FrameLayout android:id="@+id/button1"
  ...
  android:background="@drawable/image1"
  >

  <ImageButton
    android:id="@+id/button2"
    android:layout_gravity="center"
    android:src="@drawable/image2" />

</FrameLayout>
于 2012-04-27T17:37:06.513 回答
0

请尝试使用 FrameLayout 来包含两个按钮

于 2012-04-27T17:28:48.680 回答
0

对于第二张图像,设置属性aligncenterinparent = true

于 2012-04-27T17:29:09.687 回答
0

使用框架布局。将 button1 作为填充父级,将 button 2 作为带有 layout_gravity="center" 的包装内容。看下面的代码:

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

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@android:drawable/dialog_frame" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pic_2" 
            android:layout_gravity="center"/>

    </FrameLayout>
于 2012-04-27T17:41:37.033 回答