简体   繁体   English

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

[英]Android: Adding an icon beside the text in Navigation Drawer

I already created an activity which displays navigation drawer and i want to add an icon beside the textview. 我已经创建了一个显示导航抽屉的活动,我想在textview旁边添加一个图标。 But when I run, the icon still doesn't show up.I'll post my codes and please help me for the mistakes. 但是当我运行时,该图标仍然没有显示。我将发布代码,请帮助我解决错误。 Thanks in advance. 提前致谢。

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>

MAIN ACTIVITY 主要活动

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;
}

On your MainActivity declare: 在您的MainActivity上声明:

getSupportActionBar().setDisplayShowHomeEnabled(true);

along with: 随着:

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

In the code that you have posted, at no time do you actually load an icon resource. 在您发布的代码中,您实际上从未加载图标资源。 Also, you have created arrays in your resources that you never load. 另外,您已经在资源中创建了从未加载过的数组。 Were you planning to use them to create NavDrawerItem objects using them? 您是否打算使用它们来使用它们创建NavDrawerItem对象?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM