2015-06-02 77 views
0

我正在开发我的第一个应用程序在android中,我坚持我的xml布局。我有5个按钮在我的布局下垂直放置在relativelayout下。我使用索尼xperia M,根据我的屏幕尺寸,布局看起来非常好,但对于大屏幕,布局很乱。当我在具有大屏幕尺寸的设备中运行我的应用时,按钮之间没有适当的间隙。我怎样才能让我的布局适合所有屏幕尺寸?RelativeLayout不适合所有屏幕尺寸android

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:background="@drawable/blue3" 
tools:context=".MainActivity"> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="My Tasks" 
    android:id="@+id/mytasks_btn" 
    android:onClick="mytask" 
    style="@style/btnStyleBeige" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" /> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Add Task" 
    android:id="@+id/addtask_btn" 
    android:layout_marginTop="65dp" 
    style="@style/btnStyleBeige" 
    android:onClick="addtask" 
    android:layout_below="@+id/mytasks_btn" 
    android:layout_centerHorizontal="true" /> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Show Current Location" 
    android:id="@+id/shw_loc_btn" 
    style="@style/btnStyleBeige" 
    android:onClick="shwlocn" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:layout_alignParentEnd="true" /> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Search Nearby" 
    android:id="@+id/button4" 
    android:onClick="searchnear" 
    style="@style/btnStyleBeige" 
    android:layout_below="@+id/shw_loc_btn" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="67dp" /> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    style="@style/btnStyleBeige" 
    android:text="Location Distance" 
    android:id="@+id/button7" 
    android:onClick="locdis" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentStart="true" /> 

+0

相对布局有这样的问题。我认为你应该像线性 –

回答

0

你应该考虑你的包裹按钮在另一个布局,例如LinearLayout中。如果你想让按钮平均分布在屏幕上,你应该使用这样的代码。

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

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="New Button" 
      android:id="@+id/button" android:layout_weight="1"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="New Button" 
      android:id="@+id/button2" android:layout_weight="1"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="New Button" 
      android:id="@+id/button3" android:layout_weight="1"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="New Button" 
      android:id="@+id/button4" android:layout_weight="1"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="New Button" 
      android:id="@+id/button5" android:layout_weight="1"/> 
    </LinearLayout> 
</RelativeLayout> 

非常重要的是将孩子的身高设置为match_parent并设置合适的layout_weight值。

+0

其他类型的布局谢谢..非常好.. – user2205230