2013-04-18 120 views
0

我對佈局有困難。佈局:拉伸的背景圖片

作爲我的第一個開發項目,我試圖製作一個簡單的天文臺。 我想要在背景中顯示一個天文臺的圖片,並且在應用程序的底部有開始和停止按鈕 ,並排並填充屏幕寬度。

這是我的最佳拍攝,但我不滿意。

所有小部件都放置正確,但... 我的背景被拉伸,我的按鈕似乎垂直壓縮,甚至底部的文本有點裁剪。

我發現,如果我改變這些線

android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

通過

android:layout_width="wrap_content" 
android:layout_height="wrap_content" 

背景是OK,則按鈕太..但所有部件被放置在活動的中心。

我該如何解決這個問題?

順便說一句,如果我想讓我的按鈕並排,我選擇了更好的解決方案嗎?

感謝您的幫助!查爾斯。

...現在我的XML ....

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:background="@drawable/background" 
tools:context=".Chronosv1" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:layout_weight="2" > 

<Chronometer 
    android:id="@+id/chronometer1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_weight="1" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="50" 
     android:text="START" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="50" 
     android:text="STOP" /> 
</LinearLayout> 

我試過RelativeLayout的。 我不知道如何在不使用填充的情況下使用兩個相同大小的按鈕,如果您希望您的應用在不同的屏幕上運行,我不認爲這是個好主意。

無論如何,我想出了這個,但我仍然有我的拉伸圖像和我的按鈕不具有相同的大小。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/background" 
tools:context=".Chronosv1" > 

<Chronometer 
    android:id="@+id/chronometer1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:text="Start" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_toRightOf="@id/button1" 
    android:text="Stop" /> 

+1

1)最有可能的是,你需要比'LinearLayout'更先進的東西; (2)你的「Chronometer」組件是什麼? –

+0

Chronmeter是一個Android小部件 android.widget.Chronometer –

回答

0

我終於找到了解決方案。

第一個問題:我的9補丁圖像隱藏了我的其他組件。

解決方法:在你的LinearLayout填充=「0dip」 ...這裏給了我我的回答(Android layout broken with 9-patch background

問題二的主題:加機器人爲什麼我的照片是streched而我設計的屏幕的確切大小。這是一個愚蠢的問題......這是因爲操作欄和上面的其他欄(有電池電量和其他東西)。

要擺脫這些欄(包含在主題中),請在清單中使用不同版本的主題。

我選擇:Theme.Light.NoTitleBar.Fullscreen。

已解決的問題。

0

嘗試的RelativeLayout;)我認爲這是很容易處理比LinearLayout中。也許你可以上傳一張圖片,看看它的樣子,我會給你一個相關的佈局解決方案;)

+0

我對這層有一些困難,我發現如何將這些按鈕並排放置,而不是如何填充屏幕的所有寬度。 我附上了我的屏幕快照。 –

+0

[這裏是快照](http://imageshack.us/photo/my-images/199/snapshotfv.png) –

+0

我編輯了我的消息與另一個代碼示例。 –

0

你可以試試這個嗎?應該工作IMO。 這是一種更好的/防呆的方式來獲得您的佈局。

<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:background="@drawable/background" 
tools:context=".Chronosv1" > 


<LinearLayout 
android:id="@+id/layout1" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:weightSum="2" 
android:layout_alignParentBottom="true"> 
<Button 
android:id="@+id/button1" 
android:layout_width="0dp" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
android:layout_margin="5dp" 
android:text="Start" /> 

<Button 
android:id="@+id/button2" 
android:layout_width="0dp" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
android:layout_margin="5dp" 
android:text="Stop" /> 
</LinearLayout> 
<Chronometer 
android:id="@+id/chronometer1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_above="@+id/layout1" /> 
</RelativeLayout> 
+0

對於按鈕沒關係,但背景仍然是步進的。 –

+0

我注意到最上面的標題欄(帶有應用程序名稱的那個)對背景圖片有影響......我不明白爲什麼,但是我的320x240圖片由於它而不能正確顯示。 我試圖將我的圖片轉換爲9補丁圖像。現在在Eclipse/Graphical Layout中一切正常,但在我的AVD和手機上,其他組件不再顯示出來了...... grrrrrr。 –