Java 使ImageButtons重新缩放以适应屏幕边缘
我的ImageButton缩放效果很好,但出于布局的目的,我希望它能够击中屏幕的两侧,我该如何做到这一点 ImageButton重新缩放代码:Java 使ImageButtons重新缩放以适应屏幕边缘,java,android,Java,Android,我的ImageButton缩放效果很好,但出于布局的目的,我希望它能够击中屏幕的两侧,我该如何做到这一点 ImageButton重新缩放代码: public void getScreenRes() { DisplayMetrics display = this.getResources().getDisplayMetrics(); int screenwidth = display.widthPixels; int screenheight = display.heigh
public void getScreenRes() {
DisplayMetrics display = this.getResources().getDisplayMetrics();
int screenwidth = display.widthPixels;
int screenheight = display.heightPixels;
double buttonheight = screenwidth / 2.66666667;
int buttonheightint= (int) Math.round(buttonheight);
ImageButton fbLogin = (ImageButton) findViewById(R.id.facebookLogin);
ViewGroup.LayoutParams fb = fbLogin.getLayoutParams();
fb.width = screenwidth;
fb.height = buttonheightint;
fbLogin.setLayoutParams(fb);
ImageButton instaLogin = (ImageButton) findViewById(R.id.instagramLogin);
ViewGroup.LayoutParams insta = instaLogin.getLayoutParams();
insta.width = screenwidth;
insta.height = buttonheightint;
instaLogin.setLayoutParams(insta);
}
XML:
这就是我想要的,红线是我想用imagebutton填充的未占用空间:
您是否尝试过以下方法
android:layout_width="match_parent"
android:layout_height="wrap_content"
:
FILL\u PARENT(在API级别8及更高的版本中重命名为MATCH\u PARENT),这意味着视图希望与其父视图一样大(减去填充)
如上所述,使用
android:layout\u width=“匹配父项”
android:layout\u height=“包装内容”
将使按钮与其父视图的宽度相匹配,但对于已将缩放类型指定为“fitCenter”的图像,它将保持源图像的原始纵横比并使其居中,而不是使用“fitXY”拉伸到x轴和y轴。将ImageButton宽度更改为“match_parent”,将缩放类型更改为“fitXY”例如:
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="@drawable/facebook"
android:layout_centerHorizontal="true"
android:id="@+id/facebookLogin"
android:scaleType="fitXY"
android:onClick="facebookLogin"/>
但是,只有当包含图像按钮的
布局具有布局宽度等于匹配父级时,这才有效。通常,android:layout\u AlignParentLeft=“true”应该起作用,而android:layout\u width=“匹配父级”…您可能需要设置另一个缩放类型,可能是fitXY。这也没什么区别。是否已将布局宽度设置为与父级匹配,并将scaleType设置为fitXY?如果是这种情况,请检查是否已为任何顶层父视图设置了填充或边距值。从布局来看,您可能已为最顶层的根视图设置了填充或边距
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="@drawable/facebook"
android:layout_centerHorizontal="true"
android:id="@+id/facebookLogin"
android:scaleType="fitXY"
android:onClick="facebookLogin"/>