【问题标题】:ImageButton not rescalingImageButton 不重新缩放
【发布时间】:2015-01-28 09:48:15
【问题描述】:

我的 ImageButton 并没有像代码告诉它那样重新缩放。事实上它什么也没做。

代码:

public void getScreenRes(){
    DisplayMetrics display = this.getResources().getDisplayMetrics();
    int width = display.widthPixels;
    int height = display.heightPixels;
    int buttonheight = display.heightPixels / 8;
    double buttonwidth = buttonheight * 2.66666667;
    int buttonwidthint = (int) Math.round(buttonwidth);
    ImageButton firsttimeFB = (ImageButton) findViewById(R.id.firsttime_fb);
    firsttimeFB.setMaxWidth(buttonwidthint);
    firsttimeFB.setMaxHeight(buttonheight);
}

XML

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/facebook"
    android:id="@+id/firsttime_fb"
    />

【问题讨论】:

    标签: java android


    【解决方案1】:

    问题可能是在 xml 中您将宽度和高度定义为“wrap_content”

    看看这个答案: Resize ImageButton?

    【讨论】:

    • 我改变了它,但它仍然没有做任何事情,它在设置 XML 时保持不变。我希望它动态调整到我给它的大小
    【解决方案2】:

    将最后三行代码替换为:

    ImageButton firsttimeFB = (ImageButton) findViewById(R.id.firsttime_fb);
    firsttimeFB.getLayoutParams().width=buttonwidthint;
    firsttimeFB.getLayoutParams().height=buttonheight;
    

    您应该设置宽度而不是最大宽度,并且高度相同。

    【讨论】:

    • 第二行和第三行需要方法调用
    • 没关系,我用括号'格式化了宽度和高度,但它仍然不能正确缩放。
    • 虽然缩放不正确,但仍然很遥远。图片:dl.dropboxusercontent.com/u/19296530/Valour/error1.png
    • 检查按钮高度中的像素。
    • 什么意思?它应该是显示的总像素高度除以 8。
    猜你喜欢
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 2011-08-24
    相关资源
    最近更新 更多