java - ImageButton不可点击 - AndroidStudio

标签 java android android-layout android-studio

我的 Android 应用程序上有 4 个 ImageButton 的声明,但到目前为止它们不可点击,这是我的类:

public class WelcomeScreen extends Activity {

ImageButton completeprofile;
ImageButton gotoportfolio;
ImageButton findfriends;
ImageButton readnews;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome_activity);

    completeprofile = (ImageButton) findViewById(R.id.completeprofile);
    gotoportfolio = (ImageButton) findViewById(R.id.gotoportfolio);
    findfriends = (ImageButton) findViewById(R.id.findfriends);
    readnews = (ImageButton) findViewById(R.id.readnews);

    completeprofile.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent i = new Intent(WelcomeScreen.this, ProfileMember.class);
            startActivity(i);
        }
    });

    gotoportfolio.setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View arg0) {
             Intent i = new Intent(WelcomeScreen.this, PortfolioMember.class);
             startActivity(i);
         }
    });

    findfriends.setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View arg0) {
             Intent i = new Intent(WelcomeScreen.this, MainActivity.class);
             startActivity(i);
         }
    });

    readnews.setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View arg0) {
             Intent i = new Intent(WelcomeScreen.this, WebActivity.class);
             startActivity(i);
         }
    });

}    }

这是我的布局:

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="70dp"
        android:layout_marginLeft="105dp">

        <ImageButton
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:id="@+id/completeprofile"
            android:background="@drawable/completeprofile"
            android:layout_marginLeft="75dp"
            android:clickable="true" />

        <ImageButton
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:id="@+id/gotoportfolio"
            android:background="@drawable/gotoportfolio"
            android:layout_marginLeft="65dp"
            android:clickable="true" />

        <ImageButton
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:id="@+id/findfriends"
            android:background="@drawable/findfriends"
            android:layout_marginLeft="65dp"
            android:clickable="true" />

        <ImageButton
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:id="@+id/readnews"
            android:background="@drawable/readnews"
            android:layout_marginLeft="65dp"
            android:clickable="true" />

    </LinearLayout>

它们显示得很完美,但到目前为止我无法点击其中任何一个,没有堆栈跟踪错误,我对此很困惑=/

有人可以解释一下吗?

提前致谢!

最佳答案

您正在使用 PNG 图像作为背景。您应该使用 android:src 属性而不是 android:background 来获取单击按钮时的触摸反馈。如果要更改背景,则必须使用 XML 可绘制选择器。请参阅docs

关于java - ImageButton不可点击 - AndroidStudio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30950215/

相关文章:

android - 删除 GridView 两侧的填充

java - 在 Javascript 中获取 userPrincipal

java - 如何在页面对象模型类-Java中使用鼠标悬停 Action ?

java - Java继承类中的构造函数可以为空吗

android - 无法签署未签名的 APK

android - 是否可以从 Activity 文件中为 LinearLayout 设置背景?

java - 使用 hadoop 进行数据分析

android - 创建一个顶部有一个半圆的 CardView

Android 不同的可绘制屏幕分辨率

android - 如何在android的 Canvas 内对齐位图?