【问题标题】:Custom action bar items listener自定义操作栏项目监听器
【发布时间】:2014-08-11 17:30:51
【问题描述】:

我为操作栏创建自定义视图: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;
        }
    }
});

但是什么都没发生

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

添加 同样在这个类中,我有 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;
}

【问题讨论】:

  • 你必须在这里提供更多。一般来说,您的代码似乎是正确的。当你使用像“什么都没发生”这样的空短语时,你的大脑应该总是触发你可以用...... LOGCAT-stacktrace 之类的内容替换那个空短语!
  • 没见过,得查一下-
  • 我在 OptionMenuWithActivity.class 中使用了这段代码。我的其他课程扩展了这个课程。我在onTouch方法之前使用Log,但也很清楚。看来,视图的 ID 不正确
  • 检查我的答案。那是麻烦制造者吗?
  • mb 它有帮助,我也有 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

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


【解决方案1】:

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

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

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

【讨论】:

  • 是的,在 OptionMenuWithActivity.class 的 onCreate() 方法中。我的其他课程扩展了 OptionMenuWithActivity
  • 那么你不需要这里的视图参考
  • 我试试这个,但没有帮助
  • 你能编辑你的整个问题并在真正的方法中显示你所有的sn-ps。感觉就像你在召唤第二个导致混乱的视图对象。
【解决方案2】:

试试这个。这是 Switch 的示例,但其他组件的方式相同

getMenuInflater().inflate(R.menu.export, menu);
MenuItem item = menu.findItem(R.id.myswitch);
Switch switchOnActionBar = (Switch)item.getActionView().findViewById(R.id.switchForActionBar);
switchOnActionBar.setOnCheckedChangeListener(this);

SwitchForActionBar 在 switch_layout.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_gravity="center_vertical"
    android:gravity="center_vertical">

    <Switch
        android:id="@+id/switchForActionBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="Manuel"
        android:textOn="Auto"
        android:gravity="center_vertical"
        android:thumb="@drawable/switch_thumb"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:background="@android:color/transparent"
        android:layout_marginRight="10dp"/>

</RelativeLayout>

这是菜单布局

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/myswitch"
        android:title=""
        android:checkable="true"
        android:showAsAction="always"
        android:actionLayout="@layout/switch_layout"
        />
</menu>

【讨论】:

    【解决方案3】:

    为 imagebutton 尝试 ontouchlistener :

    改变

    setOnClickListener(new OnClickListener()
    

    到:

    setOnTouchListener(new OnTouchListener()
    

    【讨论】:

      【解决方案4】:

      我发现哪里出错了。

      LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
      actionBar.setCustomView(view, lp);
      

      并删除此代码:

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

      我的导航列表与按钮重叠

      谢谢大家的帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多