2013-09-24 29 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