2013-09-24 28 views
3

我是初學者。我正在開發一個應用程序,面臨佈局的一個奇怪問題。 當我在小設備上運行我的應用程序時,它工作正常,但是當我在大屏幕設備上運行它時,其佈局屬性會自動更改。 請讓我知道是否有任何方法可以以編程方式爲所有屏幕尺寸創建單個佈局? 我的XML是,Android - 如何爲多個屏幕設置單一佈局

<RelativeLayout android:id="@+id/contents" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <ImageButton 
       android:id="@+id/imgBtn1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="10dp" 
       android:background="@drawable/panchangtab1" /> 

      <ImageButton 
       android:id="@+id/imgBtn2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="10dp" 
       android:layout_toRightOf="@+id/imgBtn1" 
       android:background="@drawable/horoscopetab1" /> 
</RelativeLayout> 

在這裏我得到的第一圖像確定,但第二圖像是不實際尺寸。

+0

找到屏幕大小並通過在java中設置頁邊距來修復它的代碼 –

+0

@Ashish Patil你不應該給應用程序中使用的組件設置任何固定值。將組件的寬度和高度設置爲「match_parent」,「 WRAP_CONTENT」, 「FILL_PARENT」。 – Ram

回答

0

你應該在你的佈局文件中使用match_parent和WRAP_CONTENT。在RelativeLayout或LinearLayout或bla bla中使用它們。

如果你想要的ImageView,TextView的,按鈕...覆蓋整個屏幕寬度,你應該layout_width使用FILL_PARENT。此外,如果您希望ImageView,TextView,Button ...覆蓋整個屏幕高度,則應使用fill_parent on layout_height

您可以檢查此LayoutParams

0

您可以使用match_parent,WRAP_CONTENT,FILL_PARENT等類似的屬性,不創建硬編碼的佈局文件。使用9個補丁圖像,而不是靜態的,並不是所有的情況都滿足您的條件使用9個補丁,但是當您發現某些圖像可以在9個補丁中工作,因此需要9個補丁時,這僅適用於ImageView不適用於所有組件,剩下的東西TextViews,EditText等。你可以用你自己創建,但不要創建硬編碼佈局。

1

它爲我

使用weightsum在佈局的聲明會幫助你。

 <LinearLayout 

       android:weightSum="100" //This is horizontal layout so will work for width 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" 


       > 

       <EditText 
        android:id="@+id/txt1" 
        android:layout_weight="40" //40% for text1 
        android:layout_height="40dp" 
        > 
       </EditText> 

       <EditText 
        android:id="@+id/txt2" 
        android:layout_weight="60" //60% for text2 
        android:layout_height="50dp" 

        > 
       </EditText> 
      </LinearLayout> 
+0

你知道任何關於調整寬度的相對佈局嗎? –

+0

對於相對:您應該使用match_parent和wrap_content。 –

0

您可以使用不同的限定詞來創建不同的屏幕多種佈局是這樣的:

layout-hdpi/mylayout 

你應該閱讀this