- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 BaseActivity
中使用 Navigation Drawer
创建了一个应用程序。除了 Action Bar
中的标题更改外,一切正常。标题会更改一秒钟,但在打开新 Activity 时会显示原始标题。
可能是什么错误?谢谢
基础 Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PerfilAdapter.iniciarBaseDatos(this);
perfilObj = PerfilAdapter.selectPerfil(1);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
cargarActionBar();
cargarDrawerLayout(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.menuOpcSonidos:
...
return true;
case R.id.menuOpcCambiarColor:
...
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.menuOpcSonidos)
.setTitle(getResources().getString(R.string.sonidoOnOff) + " " + perfilObj.getSonidos());
return super.onPrepareOptionsMenu(menu);
}
private void cargarActionBar() {
ActionBar actionBar = getActionBar();
int[] colores2 = Modulo.cargarColoresDrawerlayout(perfilObj.getColor());
actionBar.setBackgroundDrawable(new GradientDrawable(Orientation.BOTTOM_TOP, colores2));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
TextView textoTitulo = (TextView)findViewById(titleId);
textoTitulo.setTextColor(getResources().getColor(R.color.blanco));
textoTitulo.setTypeface(null, Typeface.BOLD);
textoTitulo.setTextSize(19);
textoTitulo.setShadowLayer(5, 0, 0, getResources().getColor(R.color.negro));
}
private void cargarDrawerLayout(Bundle b) {
mTitle = mDrawerTitle = getTitle();
textosMenuLateral = getResources().getStringArray(R.array.nav_drawer_items);
iconosMenuLateral1 = getResources()
.obtainTypedArray(R.array.iconos_menu_lateral1);
iconosMenuLateral2 = getResources()
.obtainTypedArray(R.array.iconos_menu_lateral2);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenuMain);
int[] colores = {0, 0xFFFFFFFF, 0};
mDrawerList.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colores));
mDrawerList.setDividerHeight(4);
navDrawerItems1 = new ArrayList<DrawerItem>();
navDrawerItems1.add(new DrawerItem(textosMenuLateral[0], iconosMenuLateral1.getResourceId(0, -1)));
navDrawerItems1.add(new DrawerItem(textosMenuLateral[1], iconosMenuLateral1.getResourceId(1, -1)));
navDrawerItems1.add(new DrawerItem(textosMenuLateral[2], iconosMenuLateral1.getResourceId(2, -1)));
navDrawerItems1.add(new DrawerItem(textosMenuLateral[3], iconosMenuLateral1.getResourceId(3, -1)));
navDrawerItems1.add(new DrawerItem(textosMenuLateral[4], iconosMenuLateral1.getResourceId(4, -1)));
navDrawerItems2 = new ArrayList<DrawerItem>();
navDrawerItems2.add(new DrawerItem(textosMenuLateral[0], iconosMenuLateral2.getResourceId(0, -1)));
navDrawerItems2.add(new DrawerItem(textosMenuLateral[1], iconosMenuLateral2.getResourceId(1, -1)));
navDrawerItems2.add(new DrawerItem(textosMenuLateral[2], iconosMenuLateral2.getResourceId(2, -1)));
navDrawerItems2.add(new DrawerItem(textosMenuLateral[3], iconosMenuLateral2.getResourceId(3, -1)));
navDrawerItems2.add(new DrawerItem(textosMenuLateral[4], iconosMenuLateral2.getResourceId(4, -1)));
iconosMenuLateral1.recycle();
iconosMenuLateral2.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
adapter = new DrawerListAdapter(getApplicationContext(),
navDrawerItems1,
navDrawerItems2,
perfilObj.getColor(),
pos);
mDrawerList.setAdapter(adapter);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.icono_drawer,
R.string.app_name,
R.string.app_name
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (b == null) {
opcionesPanelLateral(0);
}
}
private class SlideMenuClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setTypeface(null, Typeface.BOLD);
opcionesPanelLateral(position);
}
}
private void opcionesPanelLateral(int position) {
Intent i;
switch (position) {
case 0:
pos = 0;
break;
case 1:
i = new Intent(this, ActivitySecond.class);
mDrawerLayout.closeDrawer(mDrawerList);
startActivity(i);
pos = 1;
break;
case 2:
i = new Intent(this, ActivityThird.class);
mDrawerLayout.closeDrawer(mDrawerList);
startActivity(i);
pos = 2;
break;
case 3:
i = new Intent(this, ActivityFourth.class);
mDrawerLayout.closeDrawer(mDrawerList);
startActivity(i);
pos = 3;
break;
case 4:
break;
default:
break;
}
mDrawerList.setItemChecked(pos, true);
mDrawerList.setSelection(pos);
setTitle(textosMenuLateral[pos]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
cargarActionBar();
cargarDrawerLayout(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
public void onBackPressed() {
mDrawerLayout.closeDrawer(mDrawerList);
}
我认为错误是使用 Activity ...但我不知道如何修复
最佳答案
Android Studio 允许通过File | 添加抽屉导航到应用程序。新 | Activity 。这个“现成的”抽屉导航附带了您需要的一切:属性、方法、事件等。
更改不同 fragment 标题的后续步骤旨在与 Android Studio 的现成抽屉导航一起使用,但它们可能对其他任何人都有帮助:
抽屉导航有几个文件:一个 Activity ,一个或更多的 fragment 。打开抽屉导航的 java Activity 文件(什么你称之为“基础 Activity ”)。
找到方法onCreate。
在此方法中,添加行 mTitle = "???",其中“???”是标题我将在抽屉导航中显示的第一个 fragment 屏幕出现。
现在找到方法onNavigationDrawerItemSelected。查理,我不确定,但我相信您的名称是 opcionesPanelLateral。
在此方法中,您有一个开关。此开关打开适当的 fragment 取决于所选的选项。为每个设置标题 fragment ,转到开关中的每个 case 并添加行 mTitle = "Mytitle"; 在 frg = new my_frag(); 之前。这样,当 fragment 打开时它将显示适当的标题。
找到方法restoreActionBar。查理,我在你的代码中没有看到它也没有任何等效的方法。添加它。在其中,添加行actionBar.setTitle( mTitle);,这里是标题的实际位置设置(也许这就是你的标题消失的原因)。里面调用这个方法onCreateOptionsMenu(膨胀后)。
就是这样。我使用的是硬编码字符串,如果您遵循 Android 规则,请使用 strings.xml。
现在是我正在开发的应用程序的真实示例。接下来是我的现成抽屉导航的“BaseActivity”的原始代码(有些东西是西类牙语的,因为我是哥斯达黎加人):
package my_package;
//------------------------------------------------------------------------------
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
//------------------------------------------------------------------------------
public class menu_act extends ActionBarActivity
implements drawer_frg.NavigationDrawerCallbacks {
//------------------------------------------------------------------------------
// Fragment managing the behaviors, interactions and presentation of the navigation drawer.
private drawer_frg 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.menu_lay );
mNavigationDrawerFragment = (drawer_frg) getSupportFragmentManager().findFragmentById(
R.id.navigation_drawer );
mTitle = "Perfil"; // getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp( R.id.navigation_drawer,( DrawerLayout )
findViewById( R.id.drawer_layout ) );
// BARRA DE TÍTULO ANARANJADA.
//ActionBar actionBar = getSupportActionBar();
//actionBar.setBackgroundDrawable( new ColorDrawable( getResources().getColor( R.color.col_nar ) ) );
}
//------------------------------------------------------------------------------
@Override
public void onNavigationDrawerItemSelected ( int position ) {
Fragment frg;
// getSupportActionBar().setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM );
switch ( position )
{ case 0 : //getSupportActionBar().setCustomView( R.layout.perfil_tit );
mTitle = "Perfil";
frg = new perfil_frg();
break;
case 1 : // getSupportActionBar().setCustomView( R.layout.contactos_tit );
mTitle = "Contactos";
frg = new contactos_frg();
break;
default : frg = PlaceholderFragment.newInstance( position + 1 );
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace( R.id.container,frg ).commit();
}
//------------------------------------------------------------------------------
public void onSectionAttached ( int number ) {
switch (number) {
case 1 : mTitle = getString( R.string.mnu_opc_per ); break;
case 2 : mTitle = getString( R.string.mnu_opc_con ); break;
case 3 : mTitle = getString( R.string.mnu_opc_sal ); 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.menu_act,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;
*/
//mDrawerToggle.syncState();
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.menu_frg, container, false);
return rootView;
}
//------------------------------------------------------------------------------
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((menu_act) activity).onSectionAttached( getArguments().getInt(ARG_SECTION_NUMBER));
}
//==============================================================================
}
//------------------------------------------------------------------------------
}
关于Android:从抽屉导航更改 ActionBar 中的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22629769/
我在我的应用程序中设置了一个操作栏。我通过调用 Activity.getActionBar() 取回 ActionBar .然后我设置了我需要的所有选项卡感谢 ActionBar.addTab()和
我正在为 Android 平板电脑 (XOOM) 开发应用程序,我想知道是否可以将操作栏放在屏幕底部,而不是顶部(这是默认选项) . 谢谢。 最佳答案 如果您在 Activity 中获取 Action
我很想为我用“ActionBar Sherlock”库导入的 ActionBar 设置一个背景图像。但是,我在 Internet 上找不到任何有关如何使用 XML 独特地设置 BackgroundIm
我需要在栏下方显示子菜单,而不是在栏本身的顶部。 复制下面我的 actionbar xml 在activity中(
我已经阅读了 AppCompat 库中的工具栏及其所有功能。我在 android 开发者博客 (here) 中注意到的一些事情是: Toolbar is fully supported in AppC
我试图做一个 Espresso 测试来检查 actionbar 是否隐藏/显示,但我似乎无法弄清楚如何将它与 Espresso.onView(...) 匹配。 Afaik它没有ID,对吧? 非常感谢
我们怎样才能让操作栏和操作栏选项卡变成纯白色。我正在使用 android-support-v7-appcompat 库来支持向后兼容性。 style/Theme.AppCompat.Light.Dar
在 Galaxy S3 上,我遇到一个问题,即 ActionBar 中的一个按钮被配置为 showAsAction="always"并且同时显示在硬件溢出菜单和操作按钮上。我希望它不显示在硬件溢出菜单
我正在学习教程 https://www.youtube.com/watch?v=VVahIc8yENk我得到了错误 java.lang.NullPointerException: Attempt to
背景 我希望在 PreferenceActivity 上显示一个 ActionBar,因为它不适用于 Honeycomb 之前的设备(即使在支持库中)。 因为这样的类在支持库中不可用,所以我不能只调用
我想实现将下载数据并将其设置到 ActionBar 选项卡中的应用程序。我已经实现了它,但我还有一件事要做。我试过添加操作栏列表,像这样 one , 但 android 不允许同时使用 Tabs 和
我希望我的 ActionBar 和 Tabs 看起来像这样。 请忽略颜色 目前看起来是这样 由于我的主题,我必须在 java 中设置自定义 ActionBar。 actionBar.setDispla
我目前正在某些页面上设置 ActionBar 副标题,但在某些页面上我不想有副标题。 我可以简单地将字幕文本设置为空,但这只会删除文本。它仍然占据字幕空间,从而将标题推高。可能很难解释,所以我用图片说
import android.app.ActionBar; import android.app.FragmentTransaction; import android.content.Intent;
我正在使用 ActionBarSherlock ,并尝试隐藏/显示全屏图像的 ActionBar,使用: getSupportActionBar.hide(); 和 getSupportActionB
我在我的简单应用程序上使用 ActionBarSherlock,如果用户在主页/主要 Activity 中,我想隐藏主页按钮。我了解如何使用 setHomeButtonEnabled(false) 执
我发现自己在 3 个单独的 Activity 中为我的 actionBar (actionBarSherlock) 列表重写了相同的代码。所有 3 个都使用相同的 actionBar,它有 3 个项目
我正在使用 ActionBarSherlock 在关于 ActionBar 标签高度的类似问题中,我使用了: @style/blah1 @style/blah2 80dp .
我一直在寻找是否有任何关于如何始终在 ActionBar 下方显示 ActionBar 选项卡的信息,即使当手机横向时也是如此。我知道它会根据屏幕上是否有足够的空间来自动将选项卡放置在 ActionB
我在 ActionBar 上方显示的 ActionBar Tabs 出现了这种奇怪的行为。当我为 ActionBar 设置自定义 View 时,这恰好发生了。我正在使用 Roman Nurik 的示例
我是一名优秀的程序员,十分优秀!