我正在学习 Android 开发者网站上的基本初学者教程。现在,我描述了如何将应用程序图标作为向上按钮(见上文):http://developer.android.com/training/basics/actionbar/adding-buttons.html#UpNav
我输入“getSupportActionBar().setDisplayHomeAsUpEnabled(true);”在我的 Activity onCreate() 方法中,但在我的设备上,我的应用程序没有应用程序图标作为按钮。这是我的 Activity
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
和
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Get the message from the intent
Intent intent = this.getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
还有我的 list
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.andreas.myapplication.MainActivity" />
</activity>
</application>
和 Gradle 信息
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.andreas.myapplication"
minSdkVersion 8
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
我很困惑,因为我已经为 minSdkVersion 8 使用了正确的方法。
有谁知道,为什么 ma App 不将图标显示为向上按钮?
最好的问候
最佳答案
我遇到了同样的问题 - 启用“将主页显示为向上”会导致出现带有箭头图像的向上按钮。我已通过以下步骤将箭头替换为应用程序图标:
- 在
/drawable
中使用应用程序图标图像创建一个新的ImageAsset
。 在子 Activity 的
OnCreate()
中,添加 2 行:getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_home);
关于Android:作为向上按钮的应用程序图标不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27746636/