ImageButton图标未显示

时间:2016-09-28 10:36:30

标签: android xamarin imagebutton

我想让ImageButton的圆角和背景颜色在按下状态下改变。问题是我根本没有看到我的图标。

这是我的代码:

<ImageButton
      android:layout_width="60dp"
      android:layout_height="60dp"
      android:layout_marginTop="85dp"
      android:layout_gravity="right"
      android:layout_marginRight="20dp"
      android:background="@drawable/imgButtonStyle"
      android:src="@drawable/myIcon"
      android:id="@+id/imgButton"/>

以下是imgButtonStyle.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_pressed="true">
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
             android:shape="rectangle">
           <solid android:color="#9F1D1F" />
           <corners android:bottomRightRadius="30dp"
                    android:bottomLeftRadius="30dp"
                    android:topRightRadius="30dp"
                    android:topLeftRadius="30dp"/>
      </shape>
   </item>
   <item android:state_pressed="false">
      <shape 
             android:shape="rectangle">
            <solid android:color="#0578AF" />
            <corners android:bottomRightRadius="30dp"
                    android:bottomLeftRadius="30dp"
                    android:topRightRadius="30dp"
                    android:topLeftRadius="30dp"/>
      </shape>
  </item>
</selector>

我误解了什么吗?

感谢任何帮助。

此问题的解决方法是将图标的大小调整为ImageButton的大小。我的图标非常大(与按钮的大小相比),所以我什么都看不到它。

谢谢大家的帮助。最后,这是一个微不足道的问题。

2 个答案:

答案 0 :(得分:1)

主要活动

package com.notificationdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_layout);




    }

}

<强> demo_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <ImageButton
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="85dp"
        android:layout_gravity="right"
        android:layout_marginRight="20dp"
        android:background="@drawable/temp_selector"
        android:src="@mipmap/ic_launcher"
        android:id="@+id/imgButton"/>

</LinearLayout>

<强> temp_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="#9F1D1F" />
            <corners android:bottomRightRadius="30dp"
                android:bottomLeftRadius="30dp"
                android:topRightRadius="30dp"
                android:topLeftRadius="30dp"/>
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="#0578AF" />
            <corners android:bottomRightRadius="30dp"
                android:bottomLeftRadius="30dp"
                android:topRightRadius="30dp"
                android:topLeftRadius="30dp"/>
        </shape>
    </item>
</selector>

此处我附上了屏幕enter image description here

答案 1 :(得分:0)

检查一下,

<强> main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageButton
        android:id="@+id/imgButton"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="right"
        android:layout_marginRight="20dp"
        android:layout_marginTop="85dp"
        android:background="@drawable/imgbuttonstyle"
        android:src="@drawable/v_light" />
</LinearLayout >

<强> imgButtonStyle.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
               android:shape="rectangle" >
            <solid android:color="#9F1D1F" />
            <corners android:bottomLeftRadius="30dp"
                     android:bottomRightRadius="30dp"
                     android:topLeftRadius="30dp"
                     android:topRightRadius="30dp" />
        </shape >
    </item >
    <item android:state_pressed="false" >
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
               android:shape="rectangle" >
            <solid android:color="#0578AF" />
            <corners android:bottomLeftRadius="30dp"
                     android:bottomRightRadius="30dp"
                     android:topLeftRadius="30dp"
                     android:topRightRadius="30dp" />
        </shape >
    </item >
</selector >