android - Need to center ImageButton src, not stretch it to match the button size -
i learning develop android user interfaces. want create imagebutton set of drawables indicate different button states. created 2 pictures each button: 133 dp normal state , scaled copy (126 dp) create pushed button effect.
i painted second picture indicate image selector mechanism works @ least.
here initial state of application
when push button changes color not size expected. both images fitted same size
activity layout xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainuseractions" android:background="@android:color/transparent"> <linearlayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightsum="3" android:layout_alignparenttop="true"> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:weightsum="2" android:gravity="top"> <imagebutton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/imagebutton" android:clickable="true" android:layout_weight="1" android:src="@drawable/broom_stick_image_selection" android:background="@null" android:layout_gravity="center" android:scaletype="center" /> <imagebutton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/imagebutton2" android:layout_gravity="center" android:clickable="true" android:layout_weight="1" android:background="@null" android:src="@drawable/vacuum_selection" android:scaletype="center" /> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1"></linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1"></linearlayout> </linearlayout> </relativelayout>
drawable selection xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/broom_stick_white_pressed_rose" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/broom_stick_white" /> <!-- focused --> <item android:drawable="@drawable/broom_stick_white" /> <!-- default --> </selector>
how tell system center src images on button layout, not fit them same size?
one way accomplish wrap item in layer-list
, allows definition of offsets.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <layer-list> <item android:drawable="@drawable/broom_stick_white_pressed_rose" android:top="2dp" android:right="2dp" android:bottom="2dp" android:left="2dp" /> </layer-list> </item> <item android:drawable="@drawable/broom_stick_white" /> </selector>
Comments
Post a Comment