Android 始终使图像按钮大小为正方形。安卓
我有一个图像按钮。如果需要,我可以将其替换为自定义视图。我需要将图像按钮的高度指定为Fill_parent。使图像延伸到屏幕中可用的高度。当这样做时,我不想放松宽度。我希望它是一个最小的高度,如果大于高度,那么这不是一个问题。我不想要这么长时间。 在我这样做的同时,我还想保持纵横比。请让我知道我需要调整的参数以获得这种视图。在此方面的任何帮助都将不胜感激。谢谢你的帮助和时间 编辑:下面是完整的xml,在其中我尝试使用高度作为填充父对象的水平滚动视图,并尝试使用适合其高度的图像填充视图,并根据纵横比扩展或收缩宽度。即使图像很小,我也希望放大,以便高度始终占据水平滚动视图Android 始终使图像按钮大小为正方形。安卓,android,android-layout,android-imageview,android-image,Android,Android Layout,Android Imageview,Android Image,我有一个图像按钮。如果需要,我可以将其替换为自定义视图。我需要将图像按钮的高度指定为Fill_parent。使图像延伸到屏幕中可用的高度。当这样做时,我不想放松宽度。我希望它是一个最小的高度,如果大于高度,那么这不是一个问题。我不想要这么长时间。 在我这样做的同时,我还想保持纵横比。请让我知道我需要调整的参数以获得这种视图。在此方面的任何帮助都将不胜感激。谢谢你的帮助和时间 编辑:下面是完整的xml,在其中我尝试使用高度作为填充父对象的水平滚动视图,并尝试使用适合其高度的图像填充视图,并根据纵横
<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中把任何东西都做成一个按钮
您可以在XML中添加一个ImageView
,给它一个id
,图像源,高度为fill\u parent
,宽度为wrap\u content
,并使用android:adjustViewBounds=“true”
固定纵横比。然后,您还可以添加android:clickable=“true”
,并通过您的id
在代码中找到它。然后可以添加onClickListener
希望它能工作我建议采用不同的方法,让父布局应用
gravity=“center\u 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);
}
试试这个代码,它对我有用
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));
}
}你试过我写的东西吗?如果我明白你的意思,那代码应该。。。我的意思是,adjustViewBounds以及layout_width属性中的wrap_内容将以缩放方式固定宽度,但是如果容器为300x72,如何将图像拉伸到300x300。。。在这种情况下,它将始终为72x72,因为它首先填充最小的长度。。。。你为什么把它设为300x72?你能把它弄大一点吗?你不能有不同的高度和宽度,因为你有一个正方形,并且你保持着纵横比,再想想你的意思,就像我看到的上面设置的结果是300x72。这不是期望的结果。预期结果为300x300。我将高度设置为“填充家长”。这大约是300。并且宽度设置为包裹内容。我希望图像从72x72扩展到300x300。如果图像为600x800,我希望将其设置为300x400,因为最大高度为300。但我的要求中没有最大宽度。这能做到吗?还要提到的是,高度约为300。它必须设置为填充父项,而不是特定的dp,因为我需要设置所有类型的屏幕大小。你能编辑你的帖子并向我展示你的整个XML吗?看起来你做错了什么,因为它应该可以工作……我已经用我正在使用的整个xml进行了编辑。如果有什么问题,请告诉我。谢谢。没错,如果你选择最大值,那么你可能会创建一个按钮,比如说,比允许的绘图区域更高。编辑:我明白你说的,但它在不同的布局上对我有效,它不应该返回显示尺寸,而是返回分配给视图的区域