2012-06-16 54 views
1

我以幾個ImageButtons創建一個Android layout。我的佈局適用於小屏幕精細(例如3.6英寸到4.2英寸),但是當我用我的應用程序上的7英寸或9英寸平板電腦的layout成爲在縱向和橫向模式難看。我沒有使用ImageButton的任何像素尺寸。我怎樣才能解決這個問題?拉伸的ImageButton

在4英寸的屏幕:

on 3.8"

在7英寸的屏幕:

on 7"

這是我的佈局代碼:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="35dp" 
     android:background="#81BEF7" 
     android:orientation="vertical" 
     android:scrollbars="vertical" > 

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/buttonlayout" android:orientation="horizontal" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" 
       android:height="32dp" android:gravity="left|top" android:background="#000000" 
       android:paddingTop="2dp" android:paddingBottom="2dp"> 
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
         android:id="@+id/buttonlayout2" android:orientation="horizontal" 
         android:layout_height="wrap_content" android:gravity="left|center_vertical" 
         android:layout_gravity="left" android:layout_width="wrap_content"> 
         <ImageButton android:id="@+id/imgbtn1" 
           android:layout_width="32dp" android:layout_height="32dp" 
           android:background="@drawable/logo" /> 
         <TextView android:id="@+id/txtTest" android:layout_width="fill_parent" 
           android:layout_height="fill_parent" android:textStyle="bold" 
           android:text="Max-Apps" android:textColor="#ffffff" 
           android:textSize="15sp" android:gravity="center_vertical" 
           android:paddingLeft="5dp" /> 
         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
           android:id="@+id/buttonlayout2" android:orientation="horizontal" 
           android:layout_height="wrap_content" android:gravity="right" 
           android:layout_gravity="right" android:layout_width="fill_parent"> 
         </LinearLayout> 
       </LinearLayout> 
       <TableLayout android:id="@+id/TableLayoutTop" 
         android:layout_gravity="left" android:scrollbars="horizontal|vertical" 
         android:scrollbarAlwaysDrawVerticalTrack="true" 
         android:scrollbarAlwaysDrawHorizontalTrack="true" 
         android:layout_height="wrap_content" android:layout_width="fill_parent" 
         android:orientation="horizontal"> 
         <TableRow android:id="@+id/TableRowTop" 
           android:layout_height="wrap_content" android:layout_width="fill_parent" /> 
       </TableLayout> 
     </LinearLayout> 
</LinearLayout> 
<HorizontalScrollView 
     android:id="@+id/scrollView1" 
     android:layout_height="50dp" 
     android:layout_width="fill_parent" > 

<LinearLayout 
    android:id="@+id/select_names_tags_ll1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:background="@android:drawable/bottom_bar" 
    android:orientation="horizontal" 
    android:paddingBottom="1.0dip" 
    android:paddingLeft="4.0dip" 
    android:paddingRight="4.0dip" 
    android:paddingTop="5.0dip" 
    android:gravity="center"> 

    <Button 
     android:id="@+id/btnPageTwo" 
     android:layout_width="80dp" 
     android:layout_height="wrap_content"   
     android:padding="5dp" 
     android:text="Featured" 
     /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="130dp" 
     android:layout_height="wrap_content" 
     android:padding="5dp" 
     android:text="Category" 
     android:onClick="onClickCategory" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="130dp" 
     android:layout_height="wrap_content" 
     android:padding="5dp" 
     android:text="Top Download" 
     android:onClick="onClickList"/> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="130dp" 
     android:layout_height="wrap_content" 
     android:padding="5dp" 
     android:text="About us" /> 
</LinearLayout>  
</HorizontalScrollView> 

<ScrollView 
     android:id="@+id/scrollView2" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" > 

     <LinearLayout 
      android:id="@+id/linearLayout1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true"> 

    <LinearLayout 
     android:orientation="horizontal"   
     android:paddingLeft="4.0dip" 
     android:paddingTop="5.0dip" 
     android:paddingRight="4.0dip" 
     android:paddingBottom="1.0dip" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/TextView01"> 

     <ImageButton 
      android:id="@+id/imageButton1" 
      android:layout_width="wrap_content" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:layout_margin="2dip" 
      android:background="@drawable/winnie" 
      android:onClick="onClickPageTwo" /> 0  

     <ImageButton 
       android:id="@+id/imageButton2" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 
       android:layout_height="wrap_content" 
       android:layout_margin="2dip"         
       android:background="@drawable/games" 
       android:onClick="onClickPageTwo" /> 
    </LinearLayout> 
</RelativeLayout> 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:layout_below="@+id/TextView01"> 

     <ImageButton 
      android:id="@+id/imageButton1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/papago" 
      android:onClick="onClickPageTwo" /> 
    </LinearLayout> 
</RelativeLayout> 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true"> 

    <LinearLayout 
     android:orientation="horizontal"   
     android:paddingLeft="4.0dip" 
     android:paddingTop="1.0dip" 
     android:paddingRight="4.0dip" 
     android:paddingBottom="1.0dip" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/TextView01">   

     <ImageButton 
       android:id="@+id/imageButton3" 
       android:layout_width="fill_parent" 
      android:layout_weight="1" 
       android:layout_height="wrap_content" 
       android:layout_margin="2dip"         
       android:background="@drawable/winnie" 
       android:onClick="onClickPageTwo" /> 

     <ImageButton 
      android:id="@+id/imageButton4" 
      android:layout_width="fill_parent" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:layout_margin="2dip" 
      android:background="@drawable/mickey" 
      android:onClick="onClickPageTwo" /> 
    </LinearLayout> 
</RelativeLayout>   


<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"  
    android:layout_centerHorizontal="true"> 

    <LinearLayout 
     android:orientation="horizontal"   
     android:paddingLeft="4.0dip" 
     android:paddingTop="1.0dip" 
     android:paddingRight="4.0dip" 
     android:paddingBottom="1.0dip" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:layout_below="@+id/TextView01"> 

     <ImageButton 
      android:id="@+id/imageButton1" 
      android:layout_width="wrap_content"    
      android:layout_height="wrap_content" 
      android:layout_margin="1dip" 
      android:background="@drawable/fb"       
      android:onClick="onClickPageTwo" /> 
    </LinearLayout> 
</RelativeLayout> 
     </LinearLayout>    
    </ScrollView> 

</LinearLayout> 
+0

您是否使用了res/layout/my_layout.xml //正常屏幕大小的佈局(「默認」) res/layout-small/my_layout.xml //小屏幕大小的佈局 res/layout-large/my_layout .xml //大屏幕尺寸的佈局 res/layout-xlarge/my_layout.xml //超大屏幕尺寸的佈局 res/layout-xlarge-land/my_layout.xml //橫向過大的佈局 res/drawable-mdpi/my_icon.png //用於中等密度的位圖 res/drawable-hdpi/my_icon.png //用於高密度的位圖 res/drawable-xhdpi/my_icon.png //用於超高密度的位圖 –

+0

i不能閱讀,你能在答案中給出更多的細節和正確的格式部分?謝謝:) – melvintcs

回答

2

您應該設計佈局&可能的屏幕尺寸的圖像並將它們放置在如下所示的相應文件夾中。

res/layout/my_layout.xml // layout for normal screen size ("default") 
res/layout-small/my_layout.xml // layout for small screen size 
res/layout-large/my_layout.xml // layout for large screen size 
res/layout-xlarge/my_layout.xml // layout for extra large screen size 
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation 

res/drawable-mdpi/my_icon.png // bitmap for medium density 
res/drawable-hdpi/my_icon.png // bitmap for high density 
res/drawable-xhdpi/my_icon.png // bitmap for extra high density 

投資你的時間在閱讀http://developer.android.com/design/index.html

+0

THX的答覆,這樣系統會自動檢測分辨率,自動選擇最佳的佈局?或者我們需要設置我們的自我?目前我只有「佈局」的文件夾,我把所有的圖像在hdpi – melvintcs

+0

完美!系統會自動檢測分辨率並自動選擇最佳佈局。您只需確保將圖像放入相應的文件夾中即可。 –

2

(1)你可以把不同大小的圖像在不同文件夾我。即hdpi,mdpi,ldpi分別適用於大屏幕,中等屏幕和小尺寸屏幕。設備會根據其大小自動選擇自己的圖像。

(2)在「資源」文件夾中創建名爲「佈局 - 土地」的新文件夾,並創建具有相同名稱的XML文件(作爲主xml文件的)這個文件夾中。只需將您的主要xml文件的代碼複製並粘貼到此文件中即可。 現在創建一個方法(實際上覆蓋)「onConfigurationChanged()」,在您的活動類「的onCreate()」方法的代碼複製粘貼到它。

做上述2個步驟後,你會看到,無論設備的大小和方向是什麼,你的應用程序的佈局將永遠是正確的,按想要的。