横幅固定宽度

时间:2013-03-19 08:38:49

标签: android android-layout

我有一个带横幅的布局。我删除了所有剩下的代码。

我将其创建为图像按钮。

布局将图像作为布局,例如在hdpi中,大小为:720x1200像素,图像按钮为720x200

这是imageButton:

enter image description here

但是图像按钮没有固定宽度,但我没有发现错误!

enter image description here

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/background"
>

    <ImageButton
            android:id="@+id/sabadell_but"
            android:background="@null"
            android:contentDescription="@string/infoDesc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:src="@drawable/banner_sabadell" />  

    </LinearLayout>

如何让图像按钮固定宽度?

2 个答案:

答案 0 :(得分:1)

尝试这个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/background">

<ImageButton
        android:id="@+id/sabadell_but"
        android:background="@drawable/banner_sabadell"
        android:contentDescription="@string/infoDesc"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        />  

</LinearLayout>

由于您已使用wrap_content作为图像按钮,因此它会强制您的按钮与imageButton宽度匹配。由于您已将图像设置为src,因此不会缩放图像。为此,您可以将图像设置为android:background,而不是将其设为android:src

答案 1 :(得分:0)

android:layout_width设为match_parentImageButton