开发者社区> 问答> 正文

android imagebutton背景图片设置

gridview的布局xml有3个imagebutton,属性android:src="@drawable/on",(用background背景图片变形了,只能用src),点击后图像重叠并且变形了,怎么办?

voice.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false"
        android:drawable="@drawable/voice_on_unpress" /> <!-- pressed -->
    <item android:state_pressed="true"
        android:drawable="@drawable/voice_on_do" /> <!-- default -->
    <item android:state_pressed="true"
        android:drawable="@drawable/voice_on_do" /> <!-- default -->
    <item
        android:drawable="@drawable/voice_on_unpress" />
</selector>


voice_press.xml
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false"
        android:drawable="@drawable/voice_on_press" /> <!-- pressed -->
    <item android:state_pressed="true"
        android:drawable="@drawable/voice_on_do" /> <!-- default -->
    <item android:state_pressed="true"
        android:drawable="@drawable/voice_on_do" /> <!-- default -->
    <item
        android:drawable="@drawable/voice_on_press" />
</selector>

activity.java中

 search_voice.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View arg0) {
                           if(flag){
                              Toast.makeText(getApplicationContext(), "语音开启",Toast.LENGTH_SHORT).show();
                              search_voice.setBackgroundResource(R.drawable.vocie_press);
                              flag = false;
                           }
                           else{
                               Toast.makeText(getApplicationContext(), "语音屏蔽",Toast.LENGTH_SHORT).show();
                              search_voice.setBackgroundResource(R.drawable.vocie);
                              flag = true;
                           }

                    }
                });

展开
收起
爵霸 2016-06-20 11:34:09 3458 0
1 条回答
写回答
取消 提交回答
  • 图形变形是你image设置的scaletype引起,这涉及到图片剪裁的问题。如果图片四周是对称可伸缩,用9patch处理一下图片。

     <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="false"
            android:drawable="@drawable/voice_on_unpress" /> <!-- pressed -->
        <item android:state_pressed="true"
            android:drawable="@drawable/voice_on_do" /> <!-- default -->
    </selector>
    2019-07-17 19:43:58
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载