我正在尝试在Android的动作栏上做一些事情。

我已经在操作栏的右侧添加了新项目。

如何更改操作栏的左侧?我想改变图标和文本,我想在其他屏幕的操作栏中添加一个“返回按钮”


当前回答

我得到了非静态方法setTitle(CharSequence)不能从静态上下文错误中引用,因为我在静态PlaceholderFragment类中使用了setTitle()。我通过使用getActivity(). getactionbar()来解决它。setTitle(“新标题”);

其他回答

为此,可以采用两种方式:XML或Java。看这里:如何更改操作栏上的文本

So:

XML:

<activity android:name=".Hello_World"
              android:label="This is the Hello World Application">
</activity>

Java:

public class TitleBar extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

       setContentView(R.layout.main);


       if ( customTitleSupported ) {
           getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
           }

       final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
       if ( myTitleText != null ) {
           myTitleText.setText("NEW TITLE");

           // user can also set color using "Color" and then "Color value constant"
          // myTitleText.setBackgroundColor(Color.GREEN);
       }
 }
}
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(getString(R.string.titolo));
actionBar.setIcon(R.mipmap.ic_launcher);
actionBar.setDisplayShowHomeEnabled(true);

你可以在你的可绘制文件夹中添加任何你想要的图标,然后在你的AndroidManifest.xml文件中更改这一行:

android:icon="@drawable/ic_launcher"

来匹配你的图标的名字。或者把你的图标作为ic_launcher,如果它们是相同的图标。至于它说什么,添加或改变任何字符串匹配的res/values/strings.xml文件。然后,再次在AndroidManifest.xml文件中修改这一行:

android:label="@string/app_name"

到他们的字符串中。您必须为整个应用程序以及您想要的任何活动执行此操作,但行是相同的。

希望这能有所帮助。

您只需要添加这3行代码。将图标替换为您自己的图标。如果你想生成图标,使用这个

getSupportActionBar().setHomeAsUpIndicator(R.drawable.icon_back_arrow);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);

对于set Title:

getActionBar().setTitle("Title");

对于set图标:

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