2011-11-02 48 views
1

我使用'LinearLayout'並添加4個控件。如何避免我添加到佈局的控件中的大量內容?

 1. textView_1 - that contain the title of the application 
     2. textView_2 - that contain some dynamic text 
     3. Button  - that contain the title of the application 
     4. ImageView - that contain the application logo (image) 

我的問題是

我如何保持所有這些控件之間的關係? 我想使第一個控件成爲屏幕的10%,第二個控件是60%,第三個控件是20%,最後是10%。

我也想保持textview的字體是動態的 - 所以如果應用程序將在大屏幕(平板電腦)上運行,那麼字體將根據屏幕大小增長。

回答

3

使用layout_weight分配空間。

<LinearLayout 
    android:orientation="vertical" 
> 
    <TextView 
    android:layout_height="0dp" 
    android:layout_weight="0.1" 
    /> 
    <TextView 
    android:layout_height="0dp" 
    android:layout_weight="0.6" 
    /> 
    <Button 
    android:layout_height="0dp" 
    android:layout_weight="0.2" 
    /> 
    <ImageView 
    android:layout_height="0dp" 
    android:layout_weight="0.1" 
    /> 
</LinearLayout> 

嘗試使用「sp」來縮放字體大小。

+0

謝謝,動態字體大小呢? – Yanshof

+0

sry,我不知道那個,如果我知道的話,早就會提到。 –

+0

你可以嘗試一下「sp」,它與「dp」類似,你的字體大小會縮放,我認爲我不會嘗試這個,但你可以隨時試一試。讓我知道這是否有效,以便我可以在答案中加入。 –

1

您可以使用LinearLayout的重量屬性來獲取空間分佈。 只需給外部線性佈局android:weightSum=1.0並將高度和寬度設置爲fill_parent即可。然後給每個子視圖android:layout_height屬性的值爲0dpandroid:layout_weight屬性值低於0.該值可以用作百分比,因爲總權重和爲1.0,1.0等於100%。當你給孩子android:layout_weight=0.1時,它會佔用身高的​​10%。

但我不確定是否有可能進行自動調整文本大小。您可能必須爲此編寫自己的自定義視圖。

1

使用android:layout_weight標記,可以指定百分比。在下面的示例中,兩個按鈕分別佔可用空間的40%和60%。

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="1.0" > 

    <Button 
     android:text="left" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight=".60" /> 

    <Button 
     android:text="right" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight=".40" /> 

</LinearLayout> 
相關問題