2013-10-11 36 views
0

我有一個FrameLayout可以填充屏幕和一個SurfaceView,它也可以填充屏幕。然後我有3個不同寬度的按鈕。我想將這些按鈕垂直排列在屏幕的右邊緣,均勻分佈,以便頂部按鈕位於屏幕的頂部,中間位於中部,底部位於底部 - 同樣,我希望垂直按鈕中心彼此對齊。FrameLayout中的Android按鈕佈局

僞佈局:

<FrameLayout layout_width="match_parent" layout_height="match_parent"> 

    <SurfaceView layout_width="match_parent" layout_height="match_parent"></SurfaceView> 

    <ToggleButton android:layout_width="45dp"></ToggleButton> 
    <ToggleButton android:layout_width="67dp"></ToggleButton> 
    <ToggleButton android:layout_width="100dp"></ToggleButton> 

</FrameLayout> 

任何幫助深表感謝和這裏的什麼我試圖達到ASCII圖片,希望它幫助!

------------------------------------------- 
|         () | 
|           | 
|           | 
|           | 
|        ( ) | 
|           | 
|           | 
|           | 
|        (  )| 
------------------------------------------- 

回答

1

試試這個

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

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <SurfaceView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
     </SurfaceView> 

     <RelativeLayout 
      android:gravity="center_horizontal" 
      android:layout_gravity="right" 
      android:background="#f00" 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" > 

      <ToggleButton 
       android:layout_width="45dp" 
        android:layout_centerHorizontal="true" 
       android:layout_height="wrap_content" > 
      </ToggleButton> 

      <ToggleButton 
       android:layout_width="67dp" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" > 
      </ToggleButton> 

      <ToggleButton 
       android:layout_width="100dp" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentRight="true" > 
      </ToggleButton> 
     </RelativeLayout> 
    </FrameLayout> 

</RelativeLayout> 

附快照輸出enter image description here

0

你可以做一個佈局,然後當你在Java代碼中調用它根據的寬度剛好排列按鈕屏幕和按鈕的寬度。把第一個按鈕放在最上面,第二個按鈕放在(screenheight/2-buttonheight/2)和最後一個按鈕放在(screenheight-buttonheight)。

注意:嘗試按照屏幕大小排列按鈕,而不是根據dp或sp計算,因爲屏幕大小可能會有所不同,這會弄亂整個事情。

好運

1

試試這個

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <SurfaceView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
    </SurfaceView> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" > 

     <ToggleButton 
      android:id="@+id/toggleFirst" 
      android:layout_width="45dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" > 
     </ToggleButton> 

     <ToggleButton 
      android:id="@+id/toggleSecond" 
      android:layout_width="67dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_below="@+id/toggleFirst" > 
     </ToggleButton> 

     <ToggleButton 
      android:id="@+id/toggleThird" 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_below="@+id/toggleSecond" > 
     </ToggleButton> 
    </RelativeLayout> 
</FrameLayout>