android - 自定义操作栏项目监听器

标签 android android-actionbar listener android-custom-view

我为操作栏创建自定义 View :action_bar_bets.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="right"
    android:weightSum="2"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/actionBarProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingRight="10dp"
        android:src="@drawable/ic_action_user" />

    <ImageButton
        android:id="@+id/actionBarBets"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingRight="10dp"
        android:src="@drawable/ic_action_betslip" />

</LinearLayout>

这是我的菜单:action_bar.xml

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


    <item
        android:id="@+id/mybetsCount"
        android:actionLayout="@layout/action_bar_bets"
        android:showAsAction="always"/>

</menu>

我在代码中创建操作栏:

ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
View view = getLayoutInflater().inflate(R.layout.action_bar_bets, null);
actionBar.setCustomView(view)

我尝试将 OnClickListener 设置为 ImageButtons:

像这样:

ImageButton profile = (ImageButton) view.findViewById(R.id.actionBarProfile);
profile.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "Profile", Toast.LENGTH_SHORT).show();
    }
});

像这样:

actionBar.getCustomView().setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.actionBarProfile:
            Toast.makeText(getApplicationContext(), "Profile", Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
        }
    }
});

但是什么也没发生

你能帮我解决这个问题吗?

添加 在这个类(class)中我还有ListNavigation

PackageManager pm = getPackageManager();
String label;
try {
    ActivityInfo activityInfo = pm.getActivityInfo(getComponentName(),
            0);
    label = activityInfo.loadLabel(pm).toString();
} catch (NameNotFoundException e) {
    label = null;
    e.printStackTrace();
}

String[] navigations = { label, "Sports", "Profile" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        getBaseContext(), R.layout.custom_spinner_title_bar,
        android.R.id.text1, navigations);
adapter.setDropDownViewResource(R.layout.custom_spinner_title_bar);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
        @Override
    public boolean onNavigationItemSelected(int itemPosition,
            long itemId) {
        switch (itemPosition) {
        case 1:
            Intent intent = new Intent(getApplicationContext(),
                    MainActivity.class);
            startActivity(intent);
            break;
        case 2:
            intent = new Intent(getApplicationContext(),
                    Activity_profile.class);
            startActivity(intent);
            break;
        default:
            break;
        }
        return false;
    }
};
actionBar.setListNavigationCallbacks(adapter, navigationListener);

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.action_bar, menu);
    return true;
}

最佳答案

我不确定你在哪里初始化按钮,但是当它在 onCreate() 之类的地方时,然后替换

ImageButton profile = (ImageButton) view.findViewById(R.id.actionBarProfile);

ImageButton profile = (ImageButton) findViewById(R.id.actionBarProfile);

关于android - 自定义操作栏项目监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18336639/

相关文章:

java - Android DynamoDBMappingException : Expected S in value

android - 无法在android中的Sqlite数据库更新命令中更新多行

javascript - jQuery:无法从类定义初始化事件监听器

dart - 可以删除对象后取消内部流订阅的模式?

android - 点击FCM通知消息时如何获取消息正文?

android - 在 Android NDK - C/embed 中映射有关传感器的信号量内存值

android - 如何在每个导航选项卡的 fragment 内创建 2 个 fragment

android - 如何在android中制作自定义拆分(底部)菜单

android - 带有操作栏但没有标题的 Activity ?

javascript - ExtJs 5 - 捕获 Ext.ux.IFrame 的 keydown 事件