2012-04-20 117 views
1

我正在爲自己創建一個汽車塢站應用程序,並且我有6個按鈕可讓我將快捷方式放置到應用程序中。我設計了橫向和縱向模式的佈局。我在縱向模式下遇到屏幕尺寸問題。我有我的圖標一字排開,像這樣:如何獲得六個按鈕來根據屏幕大小調整大小?

Text up here 
------------ 
| 1 | 2 | 
------------ 
| 3 | 4 | 
------------ 
| 5 | 6 | 
------------ 

下面是按鈕的XML:

<Button 
    android:id="@+id/btnLaunchSlotOne" 
    style="@style/launchButton" 
    android:layout_width="fill_parent" 
    android:layout_height="100dp" 
    android:layout_weight="1" 
    android:layout_marginRight="8dp" 
    android:padding="10dp" 
    android:text="Navigation" 
    android:textColor="#ffffff" /> 

現在我遇到的問題是,當我測試在一個較小的屏幕尺寸的應用中,按鈕似乎不會調整大小,並且會從底部的屏幕上移開。我怎樣才能讓他們正確擴展?我以爲用「dp」照顧所有這些?

是否需要以編程方式計算屏幕大小?

感謝您的建議/幫助!

回答

3

使用類似下面的佈局,以正確地窩的按鈕:

<LinearLayout 
    android:orientation="vertical"> 
    <TextView>Put your Text here</TextView> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_weightSum="2"> 
     <LinearLayout 
      android:orientation="vertical" 
      android:layout_height="fill_parent" 
      android:layout_width="0dp" 
      android:layout_weightSum="3" 
      android:layout_weight="1"> 
      <Button 
       android:layout_weight="1"/> 
      <Button 
       android:layout_weight="1"/> 
      <Button 
       android:layout_weight="1"/> 
     </LinearLayout> 
     <LinearLayout 
      android:orientation="vertical" 
      android:layout_weightSum="3" 
      android:layout_height="fill_parent" 
      android:layout_width="0dp" 
      android:layout_weight="1"> 
      <Button 
       android:layout_weight="1"/> 
      <Button 
       android:layout_weight="1"/> 
      <Button 
       android:layout_weight="1"/> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

然後每個按鈕需要0dp的高度(這讓重量適用和水平縮放按鈕)和填充父母的寬度。

訣竅是,子元素的權重將導致設置爲0dp的維度填充到父元素的weightSum的百分比。

例如,我有一個線性佈局,weightSum 3和3個重量爲1的子按鈕。當高度設置爲0dp時,它們每個都將父母高度的1/3作爲高度。當寬度設置爲0dp時,它們每個都將佔用父級寬度的三分之一。

這是根據百分比寬度或高度組織項目的最佳方法。

另外,如果你有兩個按鈕,一個是重量2,另一個是重量1,那麼1個按鈕將是父母高度/寬度的33%,而其他父母的高度/寬度的66%。方便的小竅門,並在所有屏幕上工作。

+0

這工作完美,謝謝你的解釋! – user1347026 2012-04-20 17:16:36

+0

感謝解決方案的詳細解釋! – ivans 2013-06-03 09:02:27

2

如果您希望按鈕始終佔據全屏幕,您可以使用GridView,它將自動處理所有屏幕尺寸的縮放比例。

+0

我想有上述用於文本按鈕的截面。網格佈局僅適用於4.0我聽說,你的意思是一個GridView?如果不是全屏,我可以使用GridView嗎?我正在開發這個2.1+ – user1347026 2012-04-20 16:51:33

+0

對不起,我的意思是GridView而不是GridLayout。是的,如果不是全屏,你可以使用GridView。您可以像其他視圖一樣定位和調整它的大小,並且其所有子項都將填充整個GridView。 – marczych 2012-04-21 07:42:01

0

您可以使用下面的XML文件:::

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TextView 
android:text="Text 1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
/> 
<LinearLayout 
    android:orientation="horizantal" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:weight_sum = "2"> 
<Button android:layout_width="fill_parent" android:layout_weight="1" 
    android:layout_height="fill_parent" android:text="test" /> 
<Button android:layout_width="fill_parent" android:layout_weight="1" 
    android:layout_height="fill_parent" android:text="test" /> 
</LinearLayout> 
<LinearLayout 
    android:orientation="horizantal" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:weight_sum = "2"> 
<Button android:layout_width="fill_parent" android:layout_weight="1" 
    android:layout_height="fill_parent" android:text="test" /> 
<Button android:layout_width="fill_parent" android:layout_weight="1" 
    android:layout_height="fill_parent" android:text="test" /> 
</LinearLayout> 
<LinearLayout 
    android:orientation="horizantal" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:weight_sum = "2"> 
<Button android:layout_width="fill_parent" android:layout_weight="1" 
    android:layout_height="fill_parent" android:text="test" /> 
<Button android:layout_width="fill_parent" android:layout_weight="1" 
    android:layout_height="fill_parent" android:text="test" /> 
</LinearLayout> 


</LinearLayout> 
相關問題