2015-06-02 79 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