操作栏 - 自定义图像和文本靠近向上按钮

问题描述:

我有一个登录屏幕有两个选项在我的应用程序 - “登录”和“创建帐户”。我想实现如下的东西,例如,登录屏幕上:操作栏 - 自定义图像和文本靠近向上按钮

enter image description here

我有一个活动,另外,通过点击“UP”按钮,我要回回来。我有以下构造函数的期望活动:

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_bl__login_form_view_controller); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 

    //----------------------added source - no effect----------------------- 

    getActionBar().setDisplayShowHomeEnabled(true); 
    getActionBar().setIcon(R.drawable.circle) ; 
    getActionBar().setTitle("A title"); 


    //--------------------------------------------------------------------- 
    setTitle ("Вход") ; 
} 

而且我有UP按钮。我如何添加圈子图像和一些文本?所有教程都说,我可以在AppManifest中设置应用程序图标,但它会改变主屏幕上的应用程序图标。我如何添加文本到后退按钮?我不想为整个应用程序实现任何导航逻辑或设置父活动,因为它是一个登录屏幕,只有在授权后才会显示带有导航逻辑的主菜单。提前致谢!

setTitle将设置标题和setIcon将设置图标ActionBar

getActionBar().setTitle("Your Title"); 
getActionBar().setDisplayShowHomeEnabled(true); 
getActionBar().setHomeButtonEnabled(true); 
getActionBar().setDisplayHomeAsUpEnabled(true); 
getActionBar().setIcon(R.drawable.youricon); 

参考:Adding Up Actionhttp://developer.android.com/guide/topics/ui/actionbar.html

+0

只显示左上角的“ 2014-09-02 08:38:04

+1

),在程序onCreateOptionsMenu(菜单菜单)中,我已将所有这些参数设置为“false”。Thnx – 2014-09-02 09:23:00

什么:

getActionBar().setIcon(R.drawable.circle); 

使用getActionBar().setIcon(R.drawable.youricon)设置的图标和getActionBar().setTitle("A title");设置图标旁边的文本。

+0

我已经这样做了,但没有任何效果。我应该编辑xml样式的动作栏吗? – 2014-09-02 08:12:37

+0

你可以用xml样式来做,但它应该以我(和其他人)写的方式工作。什么不工作? – Gumbo 2014-09-02 08:25:02

+0

只出现左上角的“ 2014-09-02 08:37:07

对于图标:

getActionBar().setIcon(R.drawable.youricon); 

对于标题:

getActionBar().setTitle("A title"); 
+0

我已经这样做了,但没有任何效果。查看编辑的问题。我应该编辑xml样式的动作栏吗? – 2014-09-02 08:14:59

您可以使用自己的布局

actionBar.setDisplayShowHomeEnabled(false); 
actionBar.setHomeButtonEnabled(false); 
actionBar.setDisplayShowTitleEnabled(false); 
actionBar.setDisplayShowCustomEnabled(true); 

final ViewGroup actionBarLayout = (ViewGroup)getLayoutInflater().inflate(R.layout.yourXML, null); 
actionBarLayout.findViewById(R.id.buttonId).setOnClickListener(this); 

actionBar.setCustomView(actionBarLayout); 

它的工作对我来说使用自定义操作栏。所以我希望它为你工作。

+0

只有左边的“ 2014-09-02 08:38:32

+0

您是否为此创建了布局? – 2014-09-02 08:39:58

+0

嗯,我在菜单文件夹中为此栏自动生成了xml文件,但好像我只能调整右边 - 中的组按钮标签在里面,是不是? – 2014-09-02 08:42:11

接受的答案很好地解决了问题。我只是添加一些使用自定义操作栏布局的附加信息。

getActionBar().setTitle("Your Title"); 
getActionBar().setDisplayShowHomeEnabled(true); 
getActionBar().setHomeButtonEnabled(true); 
getActionBar().setDisplayHomeAsUpEnabled(true); 
getActionBar().setIcon(R.drawable.youricon); 
// You can use setLogo instead like 
// getActionBar().setIcon(R.drawable.youricon); 

// In case of support action bar, if its not showing properly, you need to add display options 
//getActionBar().setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_CUSTOM); 

现在这里是描述如何在android操作栏中使用自定义布局的代码段。

// Make a layout named 'custom_actionbar' and simply add elements you want to show in your actionbar. 
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater() 
       .inflate(R.layout.custom_actionbar, null); 
// Now for support action bar, get the action bar 
actionBar = getSupportActionBar(); 

// Set necessary attributes 
actionBar.setDisplayHomeAsUpEnabled(true); 
actionBar.setHomeButtonEnabled(true); 
actionBar.setElevation(0); 

// Set the custom layout in actionbar here 
actionBar.setCustomView(actionBarLayout); 
actionBar.setDisplayOptions(actionBar.getDisplayOptions() 
       | ActionBar.DISPLAY_SHOW_CUSTOM); 

// Now handle the elements of the custom layout here 
Button b1 = (Button) findViewById(R.id.button1); 
// Set actions for Button 1 here 
b1.setText("Button 1"); 

// Another element in custom actionbar 
ImageView iv1 = (ImageView) findViewById(R.id.myImage); 
iv1.setBackgroundResource(R.drawable.myImagePlaceHolder); 

放置在onCrate,并检查自己的代码。