2014-01-26 50 views
7

我想創建一個支持所有屏幕尺寸的XML佈局。在XML中,第一個元素是ImageView,第二個元素是TextView,第三個元素是帶有Image的Button。所以TextView應該是所有設備中的確切位置(小,中,大,xLarge等)。
所有設備的Android XML佈局(小/普通/大/ XLarge等)

我該怎麼做?

下面是XML輸出應該是這樣的:

enter image description here

這裏是我的正常/中等佈局創建的XML文件:

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

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/firstscreenimage" /> 

<RelativeLayout 
    android:id="@+id/relativeLayout2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" > 

    <EditText 
     android:id="@+id/campa" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/textView3" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:gravity="center" 
     android:text="My Current\n Campaign" /> 
</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/relativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/relativeLayout2" 
    android:layout_alignParentLeft="true" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="210dp" 
     android:layout_height="210dp" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:background="@drawable/animation0" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button1" 
     android:layout_alignBottom="@+id/button1" 
     android:layout_centerHorizontal="true" 
     android:gravity="center" 
     android:text="Start" /> 
</RelativeLayout> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/relativeLayout1" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="42dp" 
    android:gravity="right" 
    android:paddingRight="25dp" 
    android:text="0.0km" 
    android:textSize="30dp" /> 

</RelativeLayout> 

回答

1

所以,你需要創建不同的文件夾並保持這些文件夾中的所有xml。

以下是應用程序中的資源目錄列表,它爲不同的屏幕尺寸提供不同的版面設計,併爲中等,高和超高密度屏幕提供不同的位圖繪圖。

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 

欲瞭解更多信息請參閱本link

+0

我用它哥哥,但它不是完全一樣的位置,我想要的。請幫我哥哥。 –

22

你需要把所有的WidthsHeightsPaddingsMargins等在/res/values/dimens.xml文件是這樣的:

dimens.xml

<!-- Small Dimensions = "Medium Dimensions * 0.75" For Example: 210*.75 = 157.5--> 
<dimen name = "button1_width_small">157.5dip</dimen> 

<!-- Medium Dimensions --> 
<dimen name = "button1_width_medium">210dip</dimen> 

<!-- Large Dimensions = "Medium Dimensions * 1.5" For Example: 210*1.5 = 315 --> 
<dimen name = "button1_width_large">315dip</dimen> 

<!-- XLarge Dimensions = "Medium Dimensions * 2" For Example: 210*1.5 = 420 --> 
<dimen name = "button1_width_xLarge">420dip</dimen> 

而在你Layouts(普通/中)使用它們像這樣:

<Button 
    android:id="@+id/button1" 
    android:layout_width="@dimen/button1_width_medium" 
    android:layout_height="210dp" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:background="@drawable/animation0" /> 

轉換用的尺寸使用以下值:

0.75 - ldpi (small) //mdpi dimens *0.75 
1.0 - mdpi (normal) //First create these dimensions 
1.5 - hdpi (large) //mdpi dimens *1.5 
2.0 - xhdpi (xLarge) //mdpi dimens *2.0 

您還需要在res創建不同Layouts Folders所有設備的文件夾並相應地使用這些尺寸。

通用佈局文件夾(Android Dev Guide):

res/layout-small 
res/layout-normal 
res/layout-large 
res/layout-xlarge 


你用讓你完成後Normal/Medium Layouts請按照下列步驟操作:

  1. 轉換的普通尺寸其他屏幕尺寸。
  2. 將您的Normal Layout xml文件複製到其他文件夾中。
  3. 根據您所在的文件夾更改所用尺寸的後綴。
  4. 調整您的可繪製文件夾中的圖像資源(寬度和高度 - 與我們用於轉換維度相同的技術)並將它們放入其各自的可繪製文件夾(drawable-ldpi,drawable-mdpi,drawable-hdpi,drawable- xdpi等)。

然後你的佈局應該在每個設備上都能正確定位。
我希望這有助於。

+0

我認爲你已經創建了正常尺寸的佈局,你只需要像這樣乘以它們就可以得到所需的尺寸: '小尺寸=正常尺寸* 0.75 大尺寸=正常尺寸* 1.5 xlarge尺寸=正常維度* 2.0' –

+0

我已經創建了res/layout-small,res/layout-normal,res/layout-large, res/layout-xlarge,但是我怎樣才能使用這個(Small Dimensions = Normal dimens * 0.75)in xml? –

+0

你必須計算你自己的這些值,並把它們放在維度文件中,檢查上面'dimens.xml:'Heading下面的答案,'中值爲210dip',然後'小值爲157.5dip',這相當於(210 * 0.75)等等。 –

1

您還需要支持顯示xml文件中的不同屏幕 打開「AndroidManifest」並在android versionName後添加以下內容。

android:versionName="1.0" > 

<supports-screens android:smallScreens="true" 

       android:normalScreens="true" 

       android:largeScreens="true" 

       android:xlargeScreens="true" 

       android:anyDensity="true" 

       android:resizeable="true"/> 
+0

<支撐屏機器人:smallScreens = 「真」 機器人:normalScreens = 「真」 機器人:largeScreens = 「真」 機器人:xlargeScreens = 「真」 機器人:anyDensity = 「真」 機器人:可調整大小= 「真」/> –

0

所有屏幕尺寸的一個xml佈局。添加和更改一行中的數字或元素。

<TableLayout 
     android:background="@android:color/white" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="9"> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1"> 

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight="1"> 


       <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="One" 
        android:layout_gravity="center" 
        android:textColor="#000000" 
        android:id="@+id/re" /> 
      </LinearLayout> 

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight="1"> 


       <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Two" 
        android:gravity="center" 
        android:textColor="#000000" 
        android:id="@+id/tr" /> 
      </LinearLayout> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1"> 

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight="1" > 


       <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:textColor="#000000" 
        android:text="Three" 

        android:id="@+id/t" /> 
      </LinearLayout> 

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight="1" > 


       <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Four" 
        android:layout_gravity="center" 
        android:textColor="#000000" 
        android:id="@+id/q" /> 
      </LinearLayout> 
     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1"> 

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight="1" > 


       <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Five" 
        android:layout_gravity="center" 
        android:textColor`enter code here`="#000000" 
        android:id="@+id/f" /> 
      </LinearLayout> 

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_weight="1" > 


       <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Six" 
        android:layout_gravity="center" 
        android:textColor="#000000" 
        android:id="@+id/s" /> 
      </LinearLayout> 
     </TableRow> 
    </TableLayout> 

</LinearLayout>