2014-01-09 69 views
0

我爲48*48,72*7296*96mdpi,ldpihdpi分別添加圖片。如何設置ImageButton以匹配不同設備的屏幕?

,並添加AndroidManifest.xml下面的代碼

<supports-screens 
      android:largeScreens="true" 
      android:normalScreens="true" 
      android:smallScreens="true" 
      android:anyDensity="true"/> 

第一個問題:

該應用程序會自行捕捉合適的圖片,當我做以上操作?

第二個問題:

但是如何設置在xml文件中的按鈕?

如果應用程序本身將捕捉到合適的圖片,所以我已經設置了widthheightmatch_parent如下面的代碼?

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

在此先感謝。

-------------------------------編輯------------- ---------------

<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:orientation="vertical" 
     android:background="#000000" 
     > 

     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 


      <ImageButton 
       android:id="@+id/FileButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dp" 
       android:layout_alignParentLeft="true" 
       android:src="@drawable/file_viewer"/> 



      <ImageButton 
       android:id="@+id/recordButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:layout_centerInParent="true" 
       android:src="@drawable/record" /> 



      <ImageButton 
       android:id="@+id/photo_record_mode" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="10dp" 
       android:layout_alignParentRight="true" 
       android:src="@drawable/recordmode"/> 



     </RelativeLayout> 
    </LinearLayout> 

我修改了上面的代碼。

我將APP安裝到4.7英寸和7英寸不同尺寸的設備上。

但它似乎使用相同大小的圖片。

和我從android:background更改爲android:src

它像下面的圖片

enter image description here

是否存在有什麼錯?

+0

集寬度和高度來包裝內容 – jyomin

回答

1

是的,Android將根據屏幕密度在運行時選擇正確的圖像資源。但是,您應該將layout_widthlayout_height設置爲wrap_content而不是match_parent

+0

我已經修改了代碼,並安裝了不同的APP,它似乎使用了相同大小的圖片。 – Wun

0

該應用程序將根據設備規格自動從適當的文件夾獲取資源。其次,即使您的圖像視圖設置爲匹配父項,圖像也不會填充父項,因爲您已設置了src屬性而不是背景。如果你想imageview的形象,以填補父,改變圖像按鈕聲明如下:

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

,如果你希望它採取原始大小,像這樣做:

<ImageButton 
      android:id="@+id/imageButton1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@android:drawable/btn1" /> 
+0

我已經修改了代碼,並安裝了不同的APP,它似乎使用相同大小的圖片。 – Wun

相關問題