gpt4 book ai didi

java - 如何将图标添加到抽屉导航中

转载 作者:太空宇宙 更新时间:2023-11-04 14:04:03 24 4
gpt4 key购买 nike

我对复杂的代码感到模糊,这些代码都是自定义抽屉导航。我只想创建一个仅包含图标的简单导航,并使用片段来交换用户单击导航列表时要显示的内容。

public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;

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

mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();

// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
android.app.Fragment objFragment = null;

switch (position){
case 0:
objFragment = new Overview();
mTitle = getString(R.string.title_section0);

break;
case 1:
objFragment = new Income();
mTitle = getString(R.string.title_section1);
break;
case 2:
objFragment = new Expenses();
mTitle = getString(R.string.title_section2);
break;
case 3:
objFragment = new Category();
mTitle = getString(R.string.title_section3);
break;
case 4:
objFragment = new CashConverter();
mTitle = getString(R.string.title_section4);
break;
case 5:
objFragment = new History();
mTitle = getString(R.string.title_section5);
break;
}


// update the main content by replacing fragments
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, objFragment)
.commit();
}

public void onSectionAttached(int position) {
switch (position) {
case 0:
mTitle = getString(R.string.title_section0);
break;
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
case 4:
mTitle = getString(R.string.title_section4);
break;
case 5:
mTitle = getString(R.string.title_section5);
break;
}
}

public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return 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();

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

return super.onOptionsItemSelected(item);
}
}

占位符片段

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";

/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}

最佳答案

有两种方法可以做到这一点。
1. 手动为抽屉导航添加项目。
2. 仍然使用列表适配器,但重写 getView 方法并利用 TextView 复合绘图。您可以搜索 setCompoundDrawablesRelative 了解更多信息。

我不会发布第一个选项,因为它需要更改代码的许多部分,但这里是第二个选项的示例。

NavigationDrawerFragment 类中找到 onCreateView 方法,并将下面的伪代码改编为您的代码。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = <get your navigation drawer list view here or leave it like the original>
mDrawerListView.setOnItemClickListener(<for the click listener, again leave it like the original>);

final LayoutInflater inflater1 = inflater; //this is for the child AdapterView to access the inflater

mDrawerListView.setAdapter(new ArrayAdapter<String>(
<get your context here>,
R.layout.simple_activated_list,
R.id.item,
<your array of items to be listed in the drawer>
){
@Override
public View getView(int pos, View convertView, ViewGroup parent){
super.getView(pos, convertView, parent);
if(convertView == null) {
// if the view is null, you need to inflate the view explicitly
convertView = inflater1.inflate(R.layout.simple_activated_list, parent, false);
}

TextView tv = (TextView)convertView.findViewById(R.id.item);
String str = getItem(pos); // for displaying your menu text, since you have overridden it
Drawable d; // for setting up your icon and its size
float density = getResources().getDisplayMetrics().density;
tv.setText(str); // set the text
// you can branch out by menu name or menu order (pos)
switch(str){
case "Your menu text":
// your specific menu item
d = getResources().getDrawable(R.drawable.ic_history);
break;
default:
// else
d = getResources().getDrawable(R.drawable.ic_arrow);
break;
}
// set it to 24dp x 24dp
d.setBounds(0,0,Math.round(24*density),Math.round(24*density));
tv.setCompoundDrawablesRelative(d, null, null, null); // set the icon

return convertView;
}
});

mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}

希望有帮助

关于java - 如何将图标添加到抽屉导航中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29045392/

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