2012-05-11 22 views
0

我開發了一個應用程序。我開發的尺寸是3.7in WVGA Nexus Onejava代碼如何支持多屏大小

所以,當顯示比這個屏幕大,它沒有問題。當我顯示小於3.7in時,出現了問題。

這裏是我的XML,我使用固定PX一些部件。

<LinearLayout 
    android:id="@+id/layout_content" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@id/layout_title" 
    android:background="@drawable/backrepeat" 
    android:gravity="center_vertical|center_horizontal" > 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical|center_horizontal" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="center_vertical|center_horizontal" > 

      <TableLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:gravity="center_vertical|center_horizontal" > 

       <TableRow 
        android:layout_width="fill_parent" 
        android:gravity="center_vertical|center_horizontal" 
        android:paddingBottom="10px" 
        android:paddingTop="10px" > 

        <LinearLayout 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:gravity="center_vertical|center_horizontal" > 

         <TextView 
          android:id="@+id/fixtext_name" 
          android:layout_width="80px" 
          android:layout_height="wrap_content" 
          android:gravity="right" 
          android:paddingBottom="10px" 
          android:text="姓名 :" 
          android:textColor="#000000" 
          android:textSize="30px" /> 

         <EditText 
          android:id="@+id/text_name" 
          android:layout_width="400px" 
          android:layout_height="wrap_content" 
          android:ems="15" 
          android:gravity="left" 
          android:inputType="textPersonName" 
          android:paddingBottom="10px" 
          android:textSize="30px" > 
         </EditText> 
        </LinearLayout> 
       </TableRow>       
      </TableLayout> 
     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

這是我的應用程序佈局的一個例子。總寬度爲480像素,並使用高度

FILL_PARENT

所以,高度不會有問題。

<supports-screens 
    android:anyDensity="true" 
    android:largeScreens="true" 
    android:normalScreens="true" 
    android:resizeable="true" 
    android:smallScreens="true" 
    android:xlargeScreens="true" 
    android:requiresSmallestWidthDp="480" 
    android:compatibleWidthLimitDp="480" 
    android:largestWidthLimitDp="480" > 
</supports-screens> 

這是manifest xml中的支持屏幕屬性。我已經設定好了。

問題是當我顯示在2.7in QVGA(類似SG Mini),它只顯示240px寬度而不是全屏項目。

我想問拿捏或應用程序屏幕大小調整大小應用基地?

如果可以,我需要爲所有的活動代碼是一回事嗎?

回答

0

您可以指定一個只能在小屏幕上工作的新XML文件。

1)右鍵點擊你的項目
2)選擇新建 - > Android的XML文件
3)創建一個新的佈局,作爲您的原始文件,然後按NEXT鍵入相同的名稱(不能完成)
4)您現在可以選擇限定符 - 例如「大小」並指定XML僅適用於小屏幕。 5)一旦您指定了限定符及其屬性,請按完成。
6)你現在有一個文件夾,名爲佈局SOMETHING(在這種情況下,佈局小)

這種新的佈局可以被修改,以適應小屏幕沒有在代碼中進行任何更改的XML文件。只要確保你有相同的ID和類似的名稱以避免NULLPOINTER異常。

希望這會有所幫助。

+0

這意味着我得到20 xml還需要創建一個適合小尺寸的新的?例如,原來我有a.xml和b.xml,然後我需要爲small_a.xml和small_b.xml創建一個新的。裏面的一個也需要完全複製而不改變 –

+0

新的佈局需要放到res/layout文件夾中? –

+0

java類怎麼樣?如果沒有實現,應用程序如何確定使用哪個文件夾? –

0

設計新的.xml文件。 設計.xml文件後,使用此代碼。

。java文件試試這個:

DisplayMetrics displaymetrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
screenHeight = displaymetrics.heightPixels; 
screenWidth = displaymetrics.widthPixels; 
screendensity = displaymetrics.densityDpi; 
Log.i("screenHeight",""+screenHeight); 
Log.i("screenWidth",""+screenWidth); 
Log.i("screendensity",""+screendensity); 

然後應用開關的情況下,你會通過.xml文件ID。

0

final ViewTreeObserver mLayoutObserver = mLayout.getViewTreeObserver();

mLayoutObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() 
{ 

    @Override 
    public void onGlobalLayout() 
    { 
     DisplayMetrics metrics = getResources().getDisplayMetrics(); 

     int deviceWidth = metrics.widthPixels; 

     int deviceHeight = metrics.heightPixels; 

     float widthInPercentage = ((float) 280/320) * 100; // 280 is the width of my LinearLayout and 320 is device screen width as i know my current device resolution are 320 x 480 so i'm calculating how much space (in percentage my layout is covering so that it should cover same area (in percentage) on any other device having different resolution 

     float heightInPercentage = ((float) 300/480) * 100; // same procedure 300 is the height of the LinearLayout and i'm converting it into percentage 

     int mLayoutWidth = (int) ((widthInPercentage * deviceWidth)/100); 

     int mLayoutHeight = (int) ((heightInPercentage * deviceHeight)/100); 

     LayoutParams layoutParams = new LayoutParams(mLayoutWidth, mLayoutHeight); 

     mLayout.setLayoutParams(layoutParams); 
    } 
}); 
+0

您能詳細解釋一下您提供的解決方案嗎? – abarisone

+0

而不改變XML文件,我們必須在我們的活動中添加此方法。 – Andriya