需要将ImageButton src居中,不要拉伸它以匹配按钮大小

时间:2016-04-19 22:08:09

标签: android android-5.0-lollipop android-imagebutton

我正在学习开发Android用户界面。我想创建一个带有一组drawable的ImageButton来指示不同的按钮状态。我为每个按钮创建了两张图片:正常状态为133 dp,缩放副本(126 dp)创建按钮效果。

enter image description here

我画了第二张照片,表明图像选择器机制起作用了。

以下是我的申请的初始状态

enter image description here

当我按下按钮时,它只会改变颜色,但不会改变尺寸。两个图像都适合相同的尺寸

enter image description here

活动布局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>

如何告诉系统只是将src图像置于按钮布局的中心位置,而不是让它们适合相同尺寸?

1 个答案:

答案 0 :(得分:1)

实现此目的的一种方法是将项目包装在layer-list中,以允许定义偏移。

<?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>