【发布时间】:2019-08-29 12:48:50
【问题描述】:
我在 Android 的 ToolBar 中有一个 ToolBar 和 ImageButtons。但是 ImageButtons 点击事件不起作用。 Java-XML 代码如下。
Java 代码:
Toolbar oyuntoolbar = (Toolbar)findViewById(R.id.oyuntoolbar);
setSupportActionBar(oyuntoolbar);
final ActionBar oyunacionbar = getSupportActionBar();
barmenubtn = (ImageButton)findViewById(R.id.barmenub);
barprofilbtn = (ImageButton)findViewById(R.id.barprofilb);
barmesajbtn = (ImageButton)findViewById(R.id.barmesajb);
barayarbtn = (ImageButton)findViewById(R.id.barayarb);
barcashalbtn = (ImageButton)findViewById(R.id.barcashalb);
barcashgonderbtn = (ImageButton)findViewById(R.id.barcashgonderb);
barmenubtn.setOnClickListener ( new View.OnClickListener () {
public void onClick (View barmenuv){
navoyunn.menulayout.openDrawer(GravityCompat.START);
}
});
barprofilbtn.setOnClickListener ( new View.OnClickListener () {
public void onClick (View barprofilv){
Toast.makeText(getApplicationContext(), "profil yıklandı", Toast.LENGTH_LONG).show();
startActivity(new Intent(getApplicationContext(), profil.class));
}
});
barmesajbtn.setOnClickListener ( new View.OnClickListener () {
public void onClick (View barmesajv){
startActivity(new Intent(oyunbar.this, mesaj.class));
}
});
barayarbtn.setOnClickListener ( new View.OnClickListener () {
public void onClick (View barayarv){
startActivity(new Intent(oyunbar.this, ayar.class));
}
});
barcashalbtn.setOnClickListener ( new View.OnClickListener () {
public void onClick (View barcashalv){
startActivity(new Intent(oyunbar.this, cashal.class));
}
});
barcashgonderbtn.setOnClickListener ( new View.OnClickListener () {
public void onClick (View barcashgonderv){
startActivity(new Intent(oyunbar.this, cashgonder.class));
}
});
XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="@+id/oyuntoolbar"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#15009e"
>
<ImageButton
android:id="@+id/barmenub"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:src="@drawable/menu1"
android:layout_marginStart="10dp"
/>
<ImageButton
android:id="@+id/barprofilb"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:src="@drawable/profil1"
android:layout_toEndOf="@+id/barmenub"
android:layout_marginStart="15dp"
/>
<ImageButton
android:id="@+id/barmesajb"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:src="@drawable/mesaj1"
android:layout_toEndOf="@+id/barprofilb"
android:layout_marginStart="10dp"
/>
<ImageButton
android:id="@+id/barayarb"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:src="@drawable/ayarlar1"
android:layout_toEndOf="@+id/barmesajb"
android:layout_marginStart="10dp"
/>
<ImageView
android:id="@+id/barcashiv"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:src="@drawable/cash1"
android:layout_toEndOf="@+id/barayarb"
android:layout_marginStart="25dp"
/>
<TextView
android:id="@+id/barcashtv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:layout_toEndOf="@+id/barcashiv"
android:layout_marginStart="5dp"
/>
<ImageButton
android:id="@+id/barcashgonderb"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:src="@drawable/cashgonder1"
android:layout_toEndOf="@+id/barcashtv"
android:layout_marginStart="15dp"
/>
<ImageButton
android:id="@+id/barcashalb"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:src="@drawable/cashal1"
android:layout_toEndOf="@+id/barcashgonderb"
android:layout_marginStart="10dp"
/>
</android.support.v7.widget.Toolbar>
Java-XML 代码有错误吗?
我该如何解决这个问题?
希望你能理解。我英语不好。对不起。
我需要你的帮助。
【问题讨论】:
-
如果您在点击这些
ImageButtons 时没有得到任何响应,那么您的Toolbar可能有一些内容。在这篇文章和this previous question of yours 之间,我不得不说你最终得到的View层次结构可能存在根本性的问题。你注释掉的addContentView()电话是什么?你在别处还有吗?您要传递给setContentView()的布局中有什么?请edit您的问题提供minimal reproducible example。这意味着请提供所有相关代码和布局 XML 文件。 -
我删除了 addcontentview 和 navdrawer。但问题仍然存在。
-
@MikeM。我删除了。
-
请edit您的问题提供minimal reproducible example。你一直要求我们猜测你所有的代码和 XML 是什么,真的会适得其反。
标签: android imagebutton