android - 带 ScaleType 的 ImageButton Android

标签 android imagebutton

我尝试在我的 Activity 中动态编写此 ImageButton 代码,但它看起来非常不同。 ScaleType 似乎不起作用。这里的 ImageButton 是我想要的方式:

     <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:background="@null"
            android:scaleType="fitStart"
            android:src="@drawable/rot" />

Here my Java-Code to generate this in my activity:

ImageButton field = new ImageButton(this);
                LayoutParams param = new linearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, 1.0f);
                field.setLayoutParams(param);
                field.setScaleType(ScaleType.FIT_START);
                field.setBackgroundResource(0);
                field.setAdjustViewBounds(true);
                field.setImageResource(R.drawable.rot);

有人看到我的错误吗?谢谢!

最佳答案

将 ImageButtons 的宽度设置为 fill_parent 并对紧靠左边距的图像使用 scaletype fitStart,对右侧的图像使用 fitEnd。至少就您的示例图像而言,应该可以解决问题。如果图像的比例宽度超过屏幕宽度,您可能会遇到一些间距问题,但这应该适合您。

关于android - 带 ScaleType 的 ImageButton Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17278298/

相关文章:

Android(RecognitionListener) 实时语音转文本预览

android - 图层列表中可绘制的图像按钮?

android - ExpandableListView 组行中的 ImageButton 导致不触发 onClickListener

java - 按下android中的图像按钮并按顺序获取数组中的随机图像

android - 按后退按钮返回 Fragments

android - 从 fragmentActivity 刷新 fragment UI

android - getIntExtra 仅返回默认值,而 getStringExtra 有效

android - Android 主屏幕小部件中的 ImageButton

c# - 按钮自动调整为 0px x 0px

android - 传感器的返回值始终为 0