2013-11-15 224 views
2

從Android開發開始,我想知道如何在XML(使用Android Studio)中實現此佈局,目標是4.x Android設備。如何在按比例縮放的背景圖像上定位按鈕

如何精確定位位圖背景上的4個按鈕?應該儘管位圖縮放工作。

場景: 我的應用程序主屏幕應該包含位於位圖上的4個按鈕。按鈕定位應該看起來完全像這樣,並且整個按比例放大/縮小(比例寬度/高度)以匹配屏幕空間。佈局草圖:

layout sketch

嘗試的解決方案:

  • 4按鈕在GridLayout與背景:

    <GridLayout 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:orientation="horizontal" 
        android:columnCount="2" 
        android:rowCount="2"> 
    
        <Button android:id="@+id/button1" 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:text="Button1" /> 
    
        <Button android:id="@+id/button2" 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:text="Button2" /> 
    
        <Button android:id="@+id/button3" 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:text="Button3" /> 
    
        <Button android:id="@+id/button4" 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:text="Button4" /> 
    </GridLayout> 
    
  • 4按鈕在GridLayout,在一個一個的ImageView以上RelativeLayout的。

  • 一個帶有onTouch()的ImageView,用於檢查顏色編碼的位圖。 (source here

該工作確定關於縮放的onTouch(),但我會用實際的按鈕,使鍵盤導航並按而保留的唯一解決方案/懸停影響正常工作。

如何定位按鈕,使它們始終與位圖匹配? 感謝您的任何見解。

由於標題這個問題看起來像一個愚蠢的,但我還沒有找到任何證明這可以實現。

+0

爲什麼不嘗試相對佈局,而不是網格佈局? – krishna

回答

0

你可以試試這個,希望能幫助

<ImageButton 
android:id="@+id/notificationform_btn_Approve" 
android:layout_width="0dp" 
android:layout_weight="1" 
android:layout_height="wrap_content" 
android:src="@drawable/btn_approve" 
android:onClick="btnApprove_Click" /> 

看一看here

,爲什麼你不使用的ImageButton代替:

<ImageButton android:src="@drawable/greybutton" 
     android:scaleType="fitCenter" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

,那麼你可以調整使用scaleType屬性進行縮放。唯一的缺點是你不能設置任何文本。 祝你好運

+0

我看不出這是如何幫助的,我需要在*位圖頂部的4 *正常按鈕。諷刺的是有這些規模以匹配位圖的位置和比例。 –

1

我有這個問題,我的方式繞過它。這是非常可怕的,但讓我們說你的背景圖像是500x500px,並且在像你的按鈕應該在(100,100)的圖像上,相對於該背景圖像,大小爲50x50px。當你佈局它時,你必須得到當前設備的大小,假設它的尺寸是700x800px(這些設備很可能不存在)。在這種情況下,您必須將按鈕的寬度設置爲(700/500)* 50px,高度將爲(800/500)* 50px。按鈕的位置將在(700/500)* 100和(800/500)* 100。

你幾乎縮放與背景圖像的按鈕。希望我讓自己明確:)