工具栏项目单击

时间:2015-02-17 10:09:40

标签: android android-toolbar

我在我的应用中使用工具栏。工具栏布局如下所示。我在工具栏中添加imageButton作为菜单项。 (这是一个问题吗?)由于我是Toolbar的新手,我不知道这是不是正确的方法。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_gravity="top"
android:layout_height="@dimen/abc_action_bar_default_height_material"
android:minHeight="?attr/actionBarSize"
android:gravity="right"
android:background="?attr/colorPrimaryDark">


<ImageButton
    android:src="@drawable/ic_action_social_share"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_gravity="right"
    android:layout_marginRight="8dp"
    android:background="?selectableItemBackground"
    android:id="@+id/shareButton" />

<ImageButton
    android:src="@drawable/ic_tag"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_gravity="right"
    android:layout_marginRight="8dp"
    android:background="?selectableItemBackground"
    android:id="@+id/tagButton" />

<ImageButton
    android:src="@drawable/ic_action_action_bookmark"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_gravity="right"
    android:layout_marginRight="8dp"
    android:background="?selectableItemBackground"
    android:id="@+id/bookmarkButton" />

</android.support.v7.widget.Toolbar>

onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.newsdetail_activity);
    mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mActionBarToolbar);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);
 }

onOptionItemSelected

@Override
public boolean onOptionsItemSelected(MenuItem item) {


    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            break;
        case R.id.shareButton:

            Log.d(TAG, "SHARE BUTTON");

            break;
    }
    return true;
}

工具栏中的shareButton点击未触发。有人能说出原因吗?

4 个答案:

答案 0 :(得分:10)

我最终使用以下代码

 ImageButton imageButton = (ImageButton) toolbar.findViewById(R.id.shareButton);

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });

我不知道这是否正确。但是它有效

答案 1 :(得分:0)

更改

 case R.id.shareButton:    
            Log.d(TAG, "SHARE BUTTON");    
            break;

 case android.R.id.shareButton:    
        Log.d(TAG, "SHARE BUTTON");    
        break;

答案 2 :(得分:0)

只需使用它。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);

        actionbar = getActionBar();
        actionbar.setDisplayShowCustomEnabled(true);
        actionbar.setCustomView(R.layout.action_bar_layout);

        btnClick = (Button) findViewById(R.id.btnClick);
        appIcon = (ImageView) findViewById(R.id.appIcon);
        appName = (TextView) findViewById(R.id.appName);

        appName.setOnClickListener(this);
        appIcon.setOnClickListener(this);
        btnClick.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.appName:
            Toast.makeText(LauncherActivity.this, "You Clicked on App Name", Toast.LENGTH_LONG).show();
            break;
        case R.id.appIcon:
            Toast.makeText(LauncherActivity.this, "You Clicked on App Icon", Toast.LENGTH_LONG).show();
            break;
        case R.id.btnClick:
            Toast.makeText(LauncherActivity.this, "You Clicked on Button", Toast.LENGTH_LONG).show();
            break;
        }
    }

查看我的git项目here

答案 3 :(得分:-1)

hi developers  for this I am here to help you and this is my latest research for this

so first you need to create a layout file 

and pest the following code 
 



















<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>


after this  include it into the main layout:
     
<include
         layout="@layout/tollbar_layout"
         android:id="@+id/toolbar"/>

</br>

now create a menu  folder and create a menu file 

[menu directory ][1]
[menu resource  file ][2]
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:title="logout"
        android:contentDescription="100"
        android:id="@+id/loout"
        android:visible="true"
        />
     <item
         android:title="about"
         android:id="@+id/about"
         >

     </item>
</menu>


now final step java code

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          Toolbar mtoolbar =  findViewById(R.id.toolbar);
        setSupportActionBar(mtoolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);

        return true;

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
         int  id  = item.getItemId();
         if(id ==R.id.loout){
             Toast.makeText(this, "this is working fine", Toast.LENGTH_SHORT).show();
         }else if(id == R.id.about){
             Toast.makeText(this, "About is working fine", Toast.LENGTH_SHORT).show();
         }
         return true;
    }
}


  [1]: https://i.stack.imgur.com/aMzxL.png
  [2]: https://i.stack.imgur.com/lupXI.png