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

| 我有一个图像按钮。如果需要,我可以将其替换为自定义视图。我需要将“图像按钮”的高度指定为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>
    
已邀请:
        好吧,因为您可以在android中将任何按钮设为按钮,所以它不必是
ImageButton
... 您可以在XML中添加
ImageView
,为其指定
id
,图像源,高度为
fill_parent
,宽度为
wrap_content
,并将宽高比固定为
android:adjustViewBounds=\"true\"
。然后,您还可以添加
android:clickable=\"true\"
,并在您的
id
的代码中找到它。然后您可以添加ѭ9。 希望能奏效     
        我建议采取另一种方法,让父级布局应用
gravity=\"center_horizontal\"
,下一步是使用自定义视图扩展
ImageButton
类以将其大小设置为正方形,这非常简单。 因此,创建新类,将其命名为
SquareImageButton
,并扩展
ImageButton
。添加带有上下文和属性的默认构造函数,并让其调用super函数,您无需添加任何其他内容。 现在,覆盖
onMeasure()
方法(该方法将以int值形式提供给您测量的宽度和高度),取最小值,然后(根据该方法的要求)使用新的最小值调用ѭ15so,使其成为正方形。它对我有用,如果您有点迷路,这里是代码:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int minDimension = Math.min(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(minDimension, minDimension);
}
    
        试试这个代码,对我有用。
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));
}
}     

要回复问题请先登录注册