2014-12-02 87 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