Android:在导航抽屉中的文本旁边添加一个图标

时间:2015-10-03 19:42:39

标签: android android-studio android-recyclerview android-navigation-drawer

我已经创建了一个显示导航抽屉的活动,我想在textview旁边添加一个图标。但是当我跑步时,图标仍然没有出现。我会发布我的代码,请帮我解决错误。提前谢谢。

STRINGS

<string-array name="nav_drawer_labels">
    <item>@string/nav_item_doctor</item>
    <item>@string/nav_item_hospital</item>
    <item>@string/nav_item_pharmacy</item>
    <item>@string/nav_item_clinic</item>
</string-array>

<string name="title_pharmacy">Pharmacy</string>
<string name="title_hospital">Hospital</string>
<string name="title_doctor">Doctor</string>
<string name ="title_clinic">Specialty Clinic</string>

<array name="nav_drawer_icons">
    <item>@drawable/ic_doctor</item>
    <item>@drawable/ic_hospital</item>
    <item>@drawable/ic_pharmacy</item>
    <item>@drawable/ic_clinic</item>
</array>

主要活动

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {

private static String TAG = MainActivity.class.getSimpleName();

private Toolbar mToolbar;
private FragmentDrawer drawerFragment;

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

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

    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    drawerFragment = (FragmentDrawer)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
    drawerFragment.setDrawerListener(this);

    // display the first navigation drawer view on app launch
    displayView(0);
}


@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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onDrawerItemSelected(View view, int position) {
    displayView(position);
}

private void displayView(int position) {
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position) {
        case 0:
            fragment = new FragmentDoctor();
            title = getString(R.string.title_doctor);
            break;
        case 1:
            fragment = new FragmentHospital();
            title = getString(R.string.title_hospital);
            break;
        case 2:
            fragment = new FragmentPharmacy();
            title = getString(R.string.title_pharmacy);
            break;
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.commit();

        // set the toolbar title
        getSupportActionBar().setTitle(title);
    }
}

NAVIGATION DRAWER ADAPTER

public class NavigationDrawerAdapter extends RecyclerView.Adapter<NavigationDrawerAdapter.MyViewHolder> {
List<NavDrawerItem> data = Collections.emptyList();
private LayoutInflater inflater;
private Context context;

public NavigationDrawerAdapter(Context context, List<NavDrawerItem> data) {
    this.context = context;
    inflater = LayoutInflater.from(context);
    this.data = data;
}

public void delete(int position) {
    data.remove(position);
    notifyItemRemoved(position);
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.nav_drawer_row, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    NavDrawerItem current = data.get(position);
    holder.title.setText(current.getTitle());
    holder.icon.setImageResource(current.getIcon());
}

@Override
public int getItemCount() {
    return data.size();
}

class MyViewHolder extends RecyclerView.ViewHolder {
    TextView title;
    ImageView icon;

    public MyViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.title);
        icon = (ImageView) itemView.findViewById(R.id.imageView);
    }
}

NAVDRAWERITEM

public class NavDrawerItem {
private String title;
private int icon;


public NavDrawerItem() {

}

public NavDrawerItem( String title, int icon) {
    this.title = title;
    this.icon = icon;
}
public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public int getIcon(){return  icon;}

public void setIcon(int icon){
    this.icon = icon;
}

2 个答案:

答案 0 :(得分:0)

在您的MainActivity声明:

getSupportActionBar().setDisplayShowHomeEnabled(true);

以及:

getSupportActionBar().setIcon(R.drawable.ic_launcher);

答案 1 :(得分:0)

在您发布的代码中,您实际上没有加载图标资源。此外,您已在资源中创建了从未加载的数组。您是否计划使用它们来创建NavDrawerItem个对象?