2012-07-24 99 views
1

一個讓我嘮叨了幾周的問題,嘗試了一切,但無濟於事。Android佈局在所有屏幕尺寸上都具有相同的相同尺寸

我想要做的是在每個屏幕尺寸上都有完全相同的佈局。讓我用圖片說明:

在一個2.7英寸設備: 2.7inch

在一個10.1英寸設備(用於說明目的嚴重Photoshop處理圖像): 10.1inchdesired

但與目前的Android實現,我得到這個在10.1英寸的設備上: ​​

總之,我希望我的應用程序看起來相同(相對來說)包括按鈕尺寸,文本縮放e tc等所有屏幕尺寸。我之前通過重寫View類,創建了自己的View並繪製了所有內容,但通過該方法添加視圖元素的速度非常快,並且具有「onClick」回調函數的優勢也消失了(因爲我只是以編程方式在被覆蓋的View類中引入位圖以用作按鈕)。有沒有什麼辦法可以實現這個使用android xml文件?

這裏是我的(測試)XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:gravity="center"> 

    <Button 
     android:id="@+id/button_1" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="ABCDEF" /> 
    <Button 
     android:id="@+id/button_2" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="GHI" /> 
    <Button 
     android:id="@+id/button_3" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="JKLM" /> 
</LinearLayout> 
+0

您可以通過檢測屏幕大小/分辨率以編程方式更改文本大小及其縮放? – SALMAN 2012-07-24 20:16:46

回答

1
if ((getResources().getConfiguration().screenLayout &  Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {  
     Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show(); 

    } 
    else if ((getResources().getConfiguration().screenLayout &  Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {  
     Toast.makeText(this, "Normal sized screen" , Toast.LENGTH_LONG).show(); 

    } 
    else if ((getResources().getConfiguration().screenLayout &  Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {  
     Toast.makeText(this, "Small sized screen" , Toast.LENGTH_LONG).show(); 
    } 
    else { 
     Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show(); 
    } 

,我認爲這可以幫助你,使你的應用程序看起來通過這個代碼的所有屏幕分辨率片斷,可以縮放控件類似的編程方式根據各自的屏幕尺寸。

謝謝:)

+0

好的,這看起來像一個妥協和許多工作。我會在哪裏把這段代碼剪掉?在應用程序活動的某個地方?我假設我不得不通過所有的按鈕和元素來遍歷並手動更改按鈕等文本的大小? – Maarten 2012-07-25 08:30:52