Ответ 1
после того, как в Inflater его точно так же, как вид в файле макета
поэтому вам нужно добавить android:onClick="clickEvent"
в ActionBar
файл настраиваемого макета
вот демо:
моя основная активность:
package com.example.testdemo;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private View viewList;
private Dialog dialogMarketList;
String a[] = { "a", "aa" };
private View header;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
View cView = getLayoutInflater().inflate(R.layout.header, null);
actionBar.setCustomView(cView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void clickEvent(View v) {
if (v.getId() == R.id.button1) {
Toast.makeText(MainActivity.this, "you click on button1",
Toast.LENGTH_SHORT).show();
}
if (v.getId() == R.id.button2) {
Toast.makeText(MainActivity.this, "you click on button2",
Toast.LENGTH_SHORT).show();
}
if (v.getId() == R.id.textView1) {
Toast.makeText(MainActivity.this, "you click on textView1",
Toast.LENGTH_SHORT).show();
}
}
}
мой макет header.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clickEvent"
android:text="Button 1" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clickEvent"
android:gravity="center"
android:textColor="#FFFFFF"
android:text="My action bar"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="clickEvent"
android:text="Button 2" />
</LinearLayout>