1

谁能帮我设计这个android布局?我想在抽屉工具栏上放两个图标,如下图所示。

在此处输入图像描述

这是 LeftNavigationActivity.class

public class LeftNavigationActivity extends BaseActivity
    implements NavigationView.OnNavigationItemSelectedListener {
private ImageView profilePic;
Session session;
FragmentManager fragmentManager = getSupportFragmentManager();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_left_navigation);
    this.setTitle("Home");
session = new Session(this);
if (!session.loggedIn()) {
    logOut();
}

View v = getLayoutInflater().inflate(R.layout.nav_header_left_navigation,
null);

TextView username = (TextView) v.findViewById(R.id.username);
Intent intent = getIntent();
String uname = intent.getStringExtra("username");
Toast.makeText(this, "Welcome " + uname, Toast.LENGTH_SHORT).show();
username.setText(uname);


LoansAndDebtsFragment loansAndDebtsFragment = new LoansAndDebtsFragment();
fragmentManager.beginTransaction().replace(R.id.layout_for_fragment, 

loansAndDebtsFragment, loansAndDebtsFragment.getTag()).commit();

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);


DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, 

R.string.navigation_drawer_close);

drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) 

findViewById(R.id.nav_view);

navigationView.setNavigationItemSelectedListener(this);

}

@Override
public void onBackPressed() {
    FragmentManager fragmentManager = this.getSupportFragmentManager();
    int stackCount = fragmentManager.getBackStackEntryCount();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else if (fragmentManager.getFragments() != null) {
        LoansAndDebtsFragment loansAndDebtsFragment = new 

LoansAndDebtsFragment();


fragmentManager.beginTransaction().replace(R.id.layout_for_fragment, 

loansAndDebtsFragment, loansAndDebtsFragment.getTag()).commit();

    }
}

LeftNavigationActivity 是包含导航菜单的 BaseDrawerActivity。

这是 app_bar_left_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.gebeya.jo.pyf.activities.LeftNavigationActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.NoToolBarWithABrandTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorWhite"
        app:titleTextColor="@color/colorBrand1"
app:popupTheme="@style/AppTheme.NoToolBarWithABrandTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_left_navigation" />

</android.support.design.widget.CoordinatorLayout>

这是 nav_header_left_navigation.xml

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

android:layout_width="match_parent"
android:layout_height="190dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/nav_background"
android:orientation="vertical">

<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/profile_image"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:src="@drawable/eyasu"
    android:layout_marginLeft="20dp"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    app:civ_border_color="@color/colorBrand1"
    app:civ_border_width="4dp"
    android:layout_alignParentStart="true"

    android:paddingBottom="10dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/userFullName"
    android:textSize="14sp"
    android:textColor="#FFF"
    android:textStyle="bold"
    android:gravity="left"
    android:paddingBottom="4dp"
    android:id="@+id/username"
    android:layout_above="@+id/email"
    android:layout_alignLeft="@+id/profile_image"
    android:layout_alignStart="@+id/profile_image" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/email"
    android:hint="@string/userEmail"
    android:gravity="left"
    android:layout_marginBottom="8dp"
    android:textSize="14sp"
    android:textColor="#fff"
    android:layout_alignParentBottom="true"
    android:layout_alignLeft="@+id/username"
    android:layout_alignStart="@+id/username" />

</RelativeLayout>
4

2 回答 2

0

1.menu使用您想要的菜单创建一个XMLitems并将此 XML 放入res/menu/文件夹中。

菜单.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.ferdous.advancefilter.MainActivity">

    <item
        android:id="@+id/action_notification"
        android:title="Notification"
        android:icon="@drawable/ic_action_notification"
        app:showAsAction="always" />

    <item
        android:id="@+id/action_warning"
        android:title="Notification"
        android:icon="@drawable/ic_action_warning"
        app:showAsAction="always" />
</menu>

2.在您的活动中,覆盖onCreateOptionsMenu()添加菜单项的方法和处理工具栏单击事件的Toolbar覆盖方法。onOptionsItemSelected()item

@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);
    return true;
}

@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 (id) {
        case R.id.action_notification:
            // Do something
            break;
        case R.id.action_warning:
            // Do something
            break;
    }

    return super.onOptionsItemSelected(item);
}

输出:

在此处输入图像描述

#. 如果您想badge在菜单上添加计数,item如下所示,那么您可以按照我的另一个答案Notification Badge On Action Item Android进行操作。我添加了详细的步骤和代码。

在此处输入图像描述

希望这会有所帮助~

于 2017-05-28T11:29:11.513 回答
0
  1. 打开安卓工作室
  2. 启动一个新的 Android Studio 项目
  3. 选择导航抽屉活动
  4. 在这里选择你的图标:https ://material.io/icons/
于 2017-05-28T10:11:52.290 回答