2013-10-16 41 views
0

我正在使用圖像作爲按鈕的背景。android按鈕背景拉伸而LinearLayout不是

像這樣。

<Button 
     android:id="@+id/invitefrnd_mail" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_weight="1" 
     android:background="@drawable/mailreport_button" /> 

當我移動看到它的背景繪製的圖像被拉伸,而如果我使用的,而不是按鈕的LinearLayout圖像不streach

這樣

<LinearLayout 
     android:id="@+id/invitefrnd_mail" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_weight="1" 
     android:background="@drawable/mailreport_button" /> 

任何一個可以解釋這是爲什麼?

完整佈局的LinearLayout ....對於按鈕的按鈕

<LinearLayout 
    android:id="@+id/buttons_top_invite" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:baselineAligned="false" 
    android:orientation="horizontal" 
    android:weightSum="2" > 

    <LinearLayout 
     android:id="@+id/invitefrnd_contacts" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:layout_weight="1" 
     android:background="@drawable/contactsinvite_button" /> 

    <LinearLayout 
     android:id="@+id/invitefrnd_facebook" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:layout_weight="1" 
     android:background="@drawable/facebookinvite_button" /> 
</LinearLayout> 
+1

爲什麼要設置機器人:layout_weight = 「1」? –

+0

從Button中移除android:layout_weight =「1」 – Manishika

+0

android:layout_weight =「1」看起來很奇怪,如果沒有錯的話。你能否發佈完整的佈局?謝謝。 – fasteque

回答

0

你甚至可以使用的LinearLayout,如果它工作正常更換的LinearLayout。讓onclick監聽你的線性佈局。

<LinearLayout  
    android:id="@+id/buttons_top_invite" 
    android:layout_width="match_parent" onClick="firstButton" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:baselineAligned="false" 
    android:orientation="horizontal" 
    android:weightSum="2" > 

    <LinearLayout 
     android:id="@+id/invitefrnd_contacts" 
     android:layout_width="wrap_content" onClick="secondbutton" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:layout_weight="1" 
     android:background="@drawable/contactsinvite_button" /> 

    <LinearLayout 
     android:id="@+id/invitefrnd_facebook" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:layout_weight="1" 
     android:background="@drawable/facebookinvite_button" /> 
</LinearLayout> 

在Java代碼:

public void firstbutton(View v) { 
     //your first button 
    } 

    public void secondbutton(View v){ 
     //your second button 
    } 
+0

謝謝哥們,我知道它工作正常我的問題是爲什麼發生這種情況。使用按鈕爲什麼圖像拉伸。 – Bora

+1

@SureshBora如果您在兩個按鈕中都提供重量,則佈局的整個寬度分爲兩部分。每個按鈕都會獲得空間。所以如果你的圖像寬度小於那個按鈕佔用的寬度將會拉伸。 –