gpt4 book ai didi

android - 如何在android中点击ActionBar Logo

转载 作者:行者123 更新时间:2023-11-29 01:28:31 24 4
gpt4 key购买 nike

我尝试在点击ActionBar 中的图标时做一些事情,但什么也没做。我也喜欢在点击操作栏中的图标时打开drawer_layout。如何使其可点击并处理它的按下事件?

public class MainActivity extends AppCompatActivity {
ViewPager viewPager,viewPager01;
CustomSwipAdapter swip_adapter;
CustomSwipAdapter01 swip_adapter01;

String[] menu;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
getSupportActionBar().setLogo(R.drawable.sample_01);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("");
}
catch (Exception e) {
}
viewPager = (ViewPager)findViewById(R.id.viewpager);
swip_adapter =new CustomSwipAdapter(this);
viewPager.setAdapter(swip_adapter);

/* viewPager01 = (ViewPager)findViewById(R.id.viewpager01);
swip_adapter01 =new CustomSwipAdapter01(this);
viewPager01.setAdapter(swip_adapter01);*/

menu = new String[]{"Home","E-Gift Voucher"};
dLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
dList = (ListView)findViewById(R.id.left_drawer);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);

dList.setAdapter(adapter);
dList.setSelector(android.R.color.holo_blue_dark);

dList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dLayout.closeDrawers();
Bundle args = new Bundle();
args.putString("Menu", menu[position]);
Fragment detail = new DetailFragment();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
/* getMenuInflater().inflate(R.menu.menu_main, menu);
MenuInflater inf = getMenuInflater();
inf.inflate(R.menu.main_activity_action,menu);
return super.onCreateOptionsMenu(menu);*/

MenuInflater inf = getMenuInflater();
inf.inflate(R.menu.main_activity_action,menu);
return true;//super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();

switch (item.getItemId()){
case android.R.id.home:
Toast.makeText(MainActivity.this,"Click on Logo",Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

最佳答案

layout xml

<RelativeLayout
android:id="@+id/rl_main_search_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent_semi"
tools:context="com.worldofmoms.views.fragments.search_and_explore.SearchFragment">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ActionBar_Light"/>


<android.support.v7.widget.RecyclerView
android:id="@+id/rv_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar_search"
android:background="@android:color/white"
android:visibility="gone"
/>


</RelativeLayout>

在你的 Activity 中添加onCreate()

       mToolbar = (Toolbar) view.findViewById(R.id.toolbar_search);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

and Add

 @Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();
if(id==android.R.id.home){
// home button from toolbar clicked
}
}

关于android - 如何在android中点击ActionBar Logo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32558569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com