嗨,我想在我的应用程序中创建一个带有导航抽屉的自定义 ActionBar。在那我想在右侧的一个圆圈内显示登录我的应用程序的人的脸。和左侧的导航栏。
.
它以前不适用于导航抽屉。
嗨,我想在我的应用程序中创建一个带有导航抽屉的自定义 ActionBar。在那我想在右侧的一个圆圈内显示登录我的应用程序的人的脸。和左侧的导航栏。
.
它以前不适用于导航抽屉。
@Manikandan 试试看:
您必须做的第一件事是实现并创建导航抽屉:
/res/layout/activity_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- menu-->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- slide menu -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity {
private String[] optionsMenu;
private DrawerLayout drawerLayout;
private ListView drawerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
opcionesMenu = new String[] {"Option 1", "Option 2", "Option 3"};
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.left_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(
getSupportActionBar().getThemedContext(),
android.R.layout.simple_list_item_1, optionssMenu));
}
//...
}
对于 navigationDrawer 菜单上的每个项目,您需要添加一个布局和一个片段。
fragment_1.xml(或菜单上的其他项目,fragment_2,fragment_3 ....)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TxtDetalle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fragment1" />
</LinearLayout>
及其每个 FragmentLayout 的关联类
Fragment1.java (fragment2,Fragment3, Fragment4...)
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_1, container, false);
}
}
我们已经设置了与每个选项关联的菜单和片段。下面将实现响应事件菜单所需的逻辑,以改变片段按下每个选项的形式。
这是通过实现onItemClick ListView 事件菜单控件来完成的,逻辑被添加到我们核心业务的onCreate() 方法的末尾。
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
drawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
Fragment fragment = null;
switch (position) {
case 1:
fragment = new Fragment1();
break;
case 2:
fragment = new Fragment2();
break;
case 3:
fragment = new Fragment3();
break;
}
FragmentManager fragmentManager =
getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
drawerList.setItemChecked(position, true);
tituloSeccion = opcionesMenu[position];
getSupportActionBar().setTitle(tituloSeccion);
drawerLayout.closeDrawer(drawerList);
}
});
}
好的,我们已经实现了基本功能,现在我要添加打开和关闭图标。
在 MainActivity 上。
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
tituloApp = getTitle();
drawerToggle = new ActionBarDrawerToggle(this,
drawerLayout,
R.drawable.ic_navigation_drawer,
R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(tituloSeccion);
ActivityCompat.invalidateOptionsMenu(MainActivity.this);
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(tituloApp);
ActivityCompat.invalidateOptionsMenu(MainActivity.this);
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
现在我们要在 ActionBar 上添加按钮(在您的用户图像上)*
在你的 MainActivity 上
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean menuAbierto = drawerLayout.isDrawerOpen(drawerList);
if(menuAbierto)
menu.findItem(R.id.action_search).setVisible(false);
else
menu.findItem(R.id.action_search).setVisible(true);
return super.onPrepareOptionsMenu(menu);
}
有了这个,我们满足了设计指南的大部分建议,但我们仍然允许用户通过单击操作栏菜单中的应用程序图标来打开。
为此,在 onCreate() 方法结束时会调用 setDisplayHomeAsUpEnabled icon() 和 setHomeButtonEnabled() 来限定脉动,并添加事件 onOptionsItemSelected()(负责处理操作栏上的击键,初始调用 onOptionsItemSelected 方法() 上面创建的ActionBarDrawerToggle 对象,所以如果它返回true(意味着谁管理了应用程序图标的点击)就直接出来这个方法了。
MainActivity 也是
public void onCreate(Bundle savedInstanceState) {
//...
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
//...
}
最后完成添加此方法:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
编辑所有项目:
https://github.com/sgolivernet/curso-android-src/tree/develop/android-navigationdrawer
您可以将自定义视图添加到操作栏
final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_custom_view_home);
添加把你的 imageView 和你想要的标题
getSupportActionBar().setDisplayShowHomeEnabled(false); getSupportActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_action_bar_back, null);
getSupportActionBar().setCustomView(mCustomView);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);