【问题标题】:Make Image Button Size a square always. Android使图像按钮大小始终为正方形。安卓
【发布时间】:2011-06-21 23:43:17
【问题描述】:

我有一个图像按钮。如果需要,我可以用自定义视图替换它。我需要将图像按钮的高度指定为 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 android-layout android-imageview android-image


    【解决方案1】:

    好吧,它不需要是 ImageButton,因为你可以在 android 中将任何东西变成按钮......

    您可以在您的 XML 中添加一个ImageView,给它一个id,图像源,高度为fill_parent,宽度为wrap_content,并使用android:adjustViewBounds="true" 固定纵横比。然后您也可以添加android:clickable="true" 并在您的id 的代码中找到它。然后你可以添加onClickListener

    希望有效果

    【讨论】:

    • 你试过我写的吗?如果我明白你在说什么,那代码应该做......我的意思是,layout_width 属性中的 adjustViewBounds 以及 wrap_content 将以缩放的方式固定宽度
    • 但是,如果容器是 300x72,如何将图像拉伸到 300x300... 在这种情况下,它将始终为 72x72,因为它首先填充最小长度...。 300x72?你能把它变大吗?你不能有不同的高度和宽度,因为你有一个正方形并且你保持纵横比,再想想你指的是什么
    • 我的意思是我看到上面设置的结果是 300x72。这不是预期的结果。所需的结果是 300x300。我将高度设置为“fill_parent”。这大约是 300。宽度设置为 wrap_content。我希望图像从 72x72 扩展到 300x300。如果图像是 600x800,我希望它设置为 300x400,因为最大高度为 300。但我的要求中没有最大宽度。这可以做到吗?还想提一下,高度约为 300。它必须设置为 fill_parent 而不是特定的 dp,因为我需要为所有类型的屏幕尺寸设置。
    • 你能编辑你的帖子并给我看你的整个 XML 吗?看来您做错了什么,因为它应该可以工作...
    • 我已经用我正在使用的整个 xml 进行了编辑。请让我知道是否有任何问题。谢谢。
    【解决方案2】:

    我建议采用不同的方法,让父布局应用 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);
    }
    

    【讨论】:

    • 确切地说,如果您采用最大的一个,那么您可能会创建一个比允许的绘图区域高度更大的按钮。编辑:我明白你所说的,但它在不同的布局上对我有用,它不应该返回显示尺寸,而是分配给视图的区域
    【解决方案3】:

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

    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));
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2011-01-18
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      相关资源
      最近更新 更多