2012-11-04 36 views
2

我不知道你們是否在大多數時間使用這種類型的佈局,但它使我發瘋。

例如,當我在屏幕中間放置一個按鈕,然後在按鈕旁邊放置一個textview時,IDE會重新對它們進行重新排序,並且textview會放在最上面,按鈕放在右邊。

在屏幕上使用太多元素時可能會出現問題。
從那時起我使用線性佈局,但它也有一些限制。

謝謝,我是剛剛進​​入android開發(4天開始學習),所以我希望你們可以幫助我。Eclipse相對佈局很難工作

回答

1

相對佈局已經證明了我最常用的佈局。除了真正可定製的事實(儘管可能不如LinearLayout簡單),它也相當「快速」。 「快速」意味着當您使用嵌套的LinearLayouts時會有一定的開銷,您可以通過使用RelativeLayout來避免這種開銷。具體做法是:

堅持的基本特徵是不幸的是沒有創建用戶界面的最有效的方式 。一個常見的例子是LinearLayout的濫用,這會導致層次結構中的視圖數量激增。每個視圖 - 或者更糟糕的是,每個佈局管理器 - 您將 添加到您的應用程序的代價是:初始化,佈局和繪圖變得更慢。當您嵌套多個使用權重參數 的LinearLayout時,佈局過程可能特別昂貴 需要對子進行兩次測量。

+0

你是對的,我希望它更多的是一個視覺工作室UI編輯器類型,它是快速,簡單和可靠的。 –

1

以我的經驗,你不應該使用eclipse拖拽& drop ui builder。它只會造成混亂和問題。它還不夠成熟(即使它在過去幾年取得了很大的進展)。

特別是作爲初學者,你應該直接使用xml。這會讓你更好地理解佈局機制。我不知道一個單獨的android開發人員,他正在與ide構建者認真合作。

3

我很少使用RelativeLayout作爲活動的主佈局。建議使用LinearLayout並設置android:weightSum,以便您的應用程序可以對屏幕提供更大的支持。正如您所知,運行Android的各種設備具有不同的屏幕尺寸。只有當你想在那個具有固定大小的特定位置放置該視圖時才使用RelativeLayout。

請參閱使用LinearLayouts創建有很多元素活動的這個例子中,你refered:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:baselineAligned="false" 
android:padding="@dimen/padding_small" 
android:weightSum="3" 
tools:ignore="HardcodedText" > 

<LinearLayout 
    android:id="@+id/picker_time_hour" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/picker_time_hour_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="@string/hours" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <Button 
     android:id="@+id/picker_time_hour_up" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="@dimen/padding_medium" 
     android:layout_marginRight="@dimen/padding_medium" 
     android:gravity="center" 
     android:text="+" /> 

    <EditText 
     android:id="@+id/picker_time_hour_value" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="@dimen/padding_medium" 
     android:layout_marginRight="@dimen/padding_medium" 
     android:gravity="center" 
     android:inputType="number" /> 

    <Button 
     android:id="@+id/picker_time_hour_down" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="@dimen/padding_medium" 
     android:layout_marginRight="@dimen/padding_medium" 
     android:gravity="center" 
     android:text="-" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/picker_time_minute" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/picker_time_minute_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="@string/minutes" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <Button 
     android:id="@+id/picker_time_minute_up" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="@dimen/padding_medium" 
     android:layout_marginRight="@dimen/padding_medium" 
     android:gravity="center" 
     android:text="+" /> 

    <EditText 
     android:id="@+id/picker_time_minute_value" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="@dimen/padding_medium" 
     android:layout_marginRight="@dimen/padding_medium" 
     android:gravity="center" 
     android:inputType="number" /> 

    <Button 
     android:id="@+id/picker_time_minute_down" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="@dimen/padding_medium" 
     android:layout_marginRight="@dimen/padding_medium" 
     android:gravity="center" 
     android:text="-" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/picker_time_second" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/picker_time_second_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="@string/seconds" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <Button 
     android:id="@+id/picker_time_second_up" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="@dimen/padding_medium" 
     android:layout_marginRight="@dimen/padding_medium" 
     android:gravity="center" 
     android:text="+" /> 

    <EditText 
     android:id="@+id/picker_time_second_value" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="@dimen/padding_medium" 
     android:layout_marginRight="@dimen/padding_medium" 
     android:gravity="center" 
     android:inputType="number" /> 

    <Button 
     android:id="@+id/picker_time_second_down" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="@dimen/padding_medium" 
     android:layout_marginRight="@dimen/padding_medium" 
     android:gravity="center" 
     android:text="-" /> 
</LinearLayout> 

</LinearLayout> 

,其結果是:

enter image description here

正如你可以看到我的看法是您應該使用LinearLayouts,但read this topic,它也可以幫助您選擇。

+0

謝謝,我碰巧聽說有拖放用戶界面構建器的外部程序,並將結果導出爲XML。你知道嗎? –

+0

我聽說過[Codiqa](http://www.codiqa.com/),但它不是免費的。我通過代碼創建佈局。我知道在墮落的人們傾向於使用拖放式UI構建器,但是當您有更多的經驗時,您將更容易使用代碼工作。但是取決於你。 –