0

我有下一个结果:

结果

右键:

android:adjustViewBounds="true"
android:background="@android:color/transparent"
android:scaleType="centerInside"
android:src="@drawable/xml_menu_button" 

xml_menu_button:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="false" android:drawable="@drawable/bottom_button_menu"></item>
    <item android:state_pressed="true" android:drawable="@drawable/bottom_button_menu_pressed"></item>
</selector>

第二个按钮具有与配置类似的 xml。

左侧按钮动态添加到面板。所以我通过代码设置它的资源:

ImageButton button = new ImageButton(getActivity());
button.setBackgroundColor(Color.TRANSPARENT);
button.setAdjustViewBounds(true);
button.setScaleType(ScaleType.CENTER_INSIDE);

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed}, 
                new BitmapDrawable(getResources(), PATH_TO_BUTTON_PRESSED_IMAGE));
states.addState(new int[] { },
                new BitmapDrawable(getResources(), UPATH_TO_BUTTON_IMAGE));

button.setImageDrawable(states);

面板是自定义 ViewGroup。打开布局边界后,我可以看到,测量和布局阶段按照我的意愿通过了。但是如您所见,图像大小之间存在差异。所有按钮都具有相同的大小 70x58 像素。在布局中按钮的大小为 77x77。做错了什么?

4

0 回答 0