使图像按钮大小始终为正方形。 Android的

时间:2011-06-21 23:43:18

标签: android android-layout android-imageview android-image

我有一个图像按钮。如果需要,我可以用自定义视图替换它。我需要将Image Button的高度指定为Fill_parent。这样图像就会延伸到屏幕中的可用高度。这样做时我不想宽松。我希望它是最小的高度,如果大于高度,那么这不是问题。我不想这么久。 虽然我这样做,但我也希望保持宽高比。请让我知道我需要调整的参数来获得这种视图。在这方面的任何形式的帮助表示赞赏。感谢您的帮助和时间。

编辑:下面是完整的xml,其中我尝试使用高度为fill_parent的水平滚动视图,并尝试使用适合其高度的图像填充它并展开或收缩宽高比根据纵横比。即使图像很小,我也想放大,以便高度始终占据水平滚动视图。

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="vertical" android:layout_weight="3"
        android:background="#666666">
    </LinearLayout>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="vertical" android:layout_weight="7"
        android:background="#888888">
        <HorizontalScrollView android:id="@+id/horizontalScrollView1"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <LinearLayout android:id="@+id/linearLayout1"
                android:layout_width="fill_parent" android:layout_height="fill_parent"
                android:orientation="horizontal">

                <ImageView android:src="@drawable/image1"
                    android:layout_weight="1" android:layout_width="wrap_content"
                    android:layout_height="fill_parent" android:adjustViewBounds="true"
                    android:id="@+id/attachimagebutton_2" />

                <ImageView android:src="@drawable/image2"
                    android:layout_weight="1" android:layout_width="wrap_content"
                    android:layout_height="fill_parent" android:adjustViewBounds="true"
                    android:id="@+id/attachimagebutton_2" />

            </LinearLayout>
        </HorizontalScrollView>
    </LinearLayout>
</LinearLayout>

3 个答案:

答案 0 :(得分:2)

好吧,没有必要让它成为ImageButton,因为你可以在android中创建一个按钮......

您可以在XML中添加ImageView,为其提供id,图片来源,身高为fill_parent,宽度为wrap_content并修复宽高比android:adjustViewBounds="true"。然后,您还可以添加android:clickable="true"并在id的代码中找到它。然后,您可以添加onClickListener

希望它有效

答案 1 :(得分:1)

我建议采用不同的方法,让父布局应用gravity="center_horizontal",下一步是使用自定义视图扩展ImageButton类,将其大小设置为方形,这非常简单。

因此,创建新类,我们将其命名为SquareImageButton,并扩展ImageButton。使用上下文和属性添加默认构造函数并让它调用超级函数,您不需要添加任何其他内容。

现在覆盖onMeasure()方法(它给出了int值中测量的宽度和高度),取最小值,并且(如方法所要求的)使用新的最小值调用setMeasuredDimension(w, h),以便它将是正方形。它对我有用,如果你有点失落,这就是代码:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int minDimension = Math.min(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(minDimension, minDimension);
}

答案 2 :(得分:0)

试试这段代码,它对我有用。

public class ButtonSquare extends Button {
public ButtonSquare(Context context) {
    super(context);
}

public ButtonSquare(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public ButtonSquare(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    super.onMeasure(
            MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
}

}