现在我正在使用带有碎片的导航抽屉和突出显示为红色的所选项目。 我使用粗体字来选择项目而不是突出显示。任何人都可以说我,我怎么能意识到这一点?
MainActivity
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0]));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1]));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2]));
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),navDrawerItems);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer_white, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
displayView(0);
}
}
private class SlideMenuClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
displayView(position);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new MainFragment();
break;
case 1:
fragment = new TheoryFragment();
break;
case 2:
fragment = new SettingsFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
DrawerAdapter
public class NavDrawerListAdapter extends BaseAdapter {
private Context context;
private ArrayList<NavDrawerItem> navDrawerItems;
public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
this.context = context;
this.navDrawerItems = navDrawerItems;
}
@Override
public int getCount() {
return navDrawerItems.size();
}
@Override
public Object getItem(int position) {
return navDrawerItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.drawer_list_item, null);
}
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
txtTitle.setText(navDrawerItems.get(position).getTitle());
return convertView;
}
}
UPD: 我理解如何加粗检查项目,切换到不同项目时不会删除它,并且从一开始就没有粗体样式。
更新代码
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View rowView = view;
TextView tv = (TextView) rowView.findViewById(R.id.title);
tv.setTypeface(null, Typeface.BOLD);
displayView(position);
}
答案 0 :(得分:6)
这是我的完整解决方案......
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
setNavDrawerItemNormal();
TextView txtview = ((TextView) view.findViewById(R.id.txtNav));
txtview.setTypeface(null, Typeface.BOLD);
}
public void setNavDrawerItemNormal()
{
for (int i=0; i< mDrawerListView.getChildCount(); i++)
{
View v = mDrawerListView.getChildAt(i);
TextView txtview = ((TextView) v.findViewById(R.id.txtNav));
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Light.ttf");
txtview.setTypeface(font);
}
}
在初始化应用程序时加粗导航抽屉中的第一项,我在列表适配器获取视图方法中做了...
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.navdrawer_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.txtNav);
textView.setText(values[position]);
if (position == 0) { textView.setTypeface(null, Typeface.BOLD); }
return rowView;
}
所以,在这里我检查了项目的位置是否为0(意味着它是第一项),然后将其设为粗体。整件事对我来说很完美!〜
答案 1 :(得分:0)
在您的SlideMenuClickListeners onItemClick中,将所有项目设置为Typeface.DEFAULT
,然后将您感兴趣的项目设置为Typeface.DEFAULT_BOLD
:
for (int i = 0; i < parent.getCount(); i++) {
TextView v = (TextView)parent.getChildAt(i);
if (v != null)
v.setTypeface(Typeface.DEFAULT);
}
((TextView)view).setTypeface(Typeface.DEFAULT_BOLD);
要在选择时删除突出显示,请移除mDrawerList.setChecked
方法中的mDrawerList.setSelection
和displayView
但是,请使用这种解决方案,因为我在初始化应用程序时难以对第一项进行嵌入。