【发布时间】:2015-03-01 01:10:51
【问题描述】:
我正在关注 Android 开发者网站上的基本初学者教程。现在,我描述了如何将应用程序图标作为向上按钮(见最后一段文字):http://developer.android.com/training/basics/actionbar/adding-buttons.html#UpNav
我把“getSupportActionBar().setDisplayHomeAsUpEnabled(true);”在我的活动 onCreate() 方法中,但在我的设备上,我的应用程序没有应用程序图标作为按钮。这是我的活动
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);
}
还有我的清单
<?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 不将 Icon 显示为 Up-Button?
最好的问候
【问题讨论】:
-
我猜
MainActivity不应该显示向上按钮,因为它已经是顶级活动。但是DisplayMessageActivity应该。 -
是的,不应该。但这不是关于是否应该存在向上按钮的问题。它应该显示应用程序图标,并且应用程序图标未显示在 TOPLevel 或 DisplayMessageActivity 中。
-
如果不应该,您可以在 MainActivity 中删除此行:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);。在运行时创建 TextView 并将其设置为 ContentView 的部分...在我看来至少看起来很幼稚。 -
当然是幼稚的。它是这里描述的 Android 开发者网站的开始教程:developer.android.com/training/basics/actionbar/… 问题是,它应该使用 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 显示我的图标;但它不像最后描述的那样
-
好吧,我没有在该链接中找到将 TextView 设置为内容视图的参考...除此之外,我会使用片段(1 个活动和所需的尽可能多的片段) .所以,我会使用标准的向上箭头,它是系统管理的“开箱即用”,没有其他技巧。