2015-05-02 23 views
-2

我想要創建一個應用程序,要求我在某些位置將ImageButton放在ImageView的頂部。但是由於我對Android編程比較陌生,並且使用不同的佈局,所以我一直在關注這個問題,並且到目前爲止找不到合適的解決方案。將ImageButton放在ImageView頂部,不管分辨率如何

問題是,如標題所述,ImageButtons在不同分辨率下相對於ImageView更改其位置。現在,這並不令人困惑,因爲按鈕與活動邊界相關。我還沒找到一種方法,把它們放在我的ImageView上。

因此,任何指向正確方向的指針都將是一個巨大的幫助。 感謝您花時間閱讀本文並希望瞭解我的問題所在。明天醒來後我會再回來看看。

回答

0

使用RelativeLayout這樣

 <RelativeLayout 
     android:id="@+id/bodyLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <ImageView 
      android:id="@+id/base" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/YOUR_IMAGE_SOURCE" 
      android:layout_centerInParent="true"/> 
     <ImageButton 
      android:id="@+id/overlay" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/YOUR_IMAGE_SOURCE" 
      android:layout_centerInParent="true"/> 

    </RelativeLayout> 
0

我建議你使用佈局而不是ImageViewImageButtons到它的背景,你可以設置圖像。

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:background="@drawable/content_bg"> 
    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:src="@drawable/properties"/> 
    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:src="@drawable/properties"/> 
</LinearLayout> 

不需要每次都使用一個按鈕,你可以設置OnClickListener和技術setOnTouchListener任何佈局如下圖所示。

layout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

     } 
    }); 

    layout.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
     return false; 
     } 
    }); 
相關問題