2014-12-02 78 views
0

繼我上一個問題Fitting three buttons and a background image in android之後,我現在已經有了我想要的東西,但是現在有一個我不明白的小問題。 有三個按鈕,每個按鈕都帶有一個圖像,三個圖像的大小相同,但顯示的方式不同。圖像2從左邊界開始,但圖像1和3在左側有一個邊緣。按鈕圖片的不同行爲

enter image description here

從XML文件,我不明白其中的原因:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:weightSum="4" 
    > 

    <ImageView 
     android:id="@+id/ImageView01" 
     android:layout_width="match_parent" 
     android:layout_height="5dp" 
     android:layout_weight="0.5" 
     android:scaleType="fitXY" 
     android:src="@drawable/sector1" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:drawableLeft="@drawable/agenda_izq" 
     android:maxLines="1" 
     android:text="Agenda" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="@color/fondo" 
     android:drawableLeft="@drawable/actividades_izq" 
     android:maxLines="1" 
     android:text="Actividades" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:drawableLeft="@drawable/suscribete_izq" 
     android:gravity="left|center_vertical|center_horizontal" 
     android:maxLines="2" 
     android:text="Recibir Información de Juventud" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="match_parent" 
     android:layout_height="5dp" 
     android:layout_weight="0.5" 
     android:scaleType="fitXY" 
     android:src="@drawable/sector3" /> 

</LinearLayout> 

我想所有這三個圖片來自左邊界開始,沒有任何餘量。

謝謝。

+0

圖像中必須有空格。 – 2014-12-02 07:23:18

+1

請確保,您的圖片不包含根據@MurtazaHussain所說的空白背景,另一件事是根據您的需要使用帶有weight屬性的linearlayout,這裏看起來您的計算相關的權重不正常,請正確設置它們的權重。你的問題100%。 – Radhey 2014-12-02 07:53:56

回答

1

你的第二個說明你可能會考慮以對齊文本ImageView S的權使用RelativeLayout圖像鏈接到左側,沒有空白,因爲該行:

android:background="@color/fondo" 
+0

這是,謝謝.. – novato 2014-12-02 15:01:29

1

而是具有drawableLeft按鈕,你可能想使用LinearLayout S作爲按鈕嘗試的選擇如何您的圖像和文字顯示,像這樣的更加靈活地給予:

<LinearLayout 
     android:id="@+id/button2" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:minHeight="48dp"> 

    <ImageView 
     android:id="@+id/imageViewForButton2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scaleType="fitXY" 
     android:src="@drawable/actividades_izg" /> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/fondo" 
     android:maxLines="1" 
     android:text="Actividades" /> 
</LinearLayout> 

你會再有一個上述LinearLayouts包含ImageViewTextView每個「按鈕」。爲了引用它,例如在onCreateView,你會再使用:

LinearLayout button1 = (LinearLayout) view.findViewById(R.id.button2); 

取而代之的是LinearLayoutAligning a TextView and ImageView inside a LinearLayout, Accepted Answer