我有两个图像按钮,button1和button2。对于button1,我正在设置一个更大的图像,而对于button2,我正在设置一个小图像。我必须将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>
答案 0 :(得分:2)
使用当前布局,您需要嵌套项目。但由于你只对图像按钮的图像部分感兴趣,你可以在我的例子中使用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>
答案 1 :(得分:0)
请尝试使用FrameLayout包含两个按钮
答案 2 :(得分:0)
对于第二张图片,请设置属性aligncenterinparent = true
。
答案 3 :(得分:0)
使用框架布局。将button1作为填充父项,将按钮2作为换行内容,使用layout_gravity =&#34; center&#34; 。请看下面的代码:
<?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>