gpt4 book ai didi

android - 标题栏上的后退按钮不适用于 Lollipop 之前的设备

转载 作者:太空狗 更新时间:2023-10-29 14:08:24 26 4
gpt4 key购买 nike

我在我的应用程序中的一些 Activity 的标题栏上添加了一个后退按钮。后退按钮适用于 Lollipop 设备,但当我在 Icecream Sandwich 设备上测试我的应用程序时,后退按钮不起作用。这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //Adding back button
List<ItemObjectArticle> rowListItem = getAllArticleItemList();
lLayout = new LinearLayoutManager(ArticleActivity.this);
RecyclerView rView = (RecyclerView)findViewById(R.id.recycler_view1);
rView.setLayoutManager(lLayout);
ArticleAdapter rcAdapter = new ArticleAdapter(ArticleActivity.this, rowListItem);
rView.setAdapter(rcAdapter);
}

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;
}else if(id==R.id.home){
NavUtils.navigateUpFromSameTask(this); //handling click
return true;
}

return super.onOptionsItemSelected(item);
}

在 Android Manifest 文件中,我添加了:

<activity
android:name=".ArticleView"
android:label="@string/title_activity_article_view"
android:parentActivityName="com.example.android.kheti.ArticleActivity"> //this
<intent-filter>
<action android:name="com.example.android.kheti.ARTICLEVIEW" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

我需要更改什么代码才能使其适用于所有 Android 设备?

最佳答案

来自文档

Beginning in Android 4.1 (API level 16), you can declare the logical parent of each activity by specifying the android:parentActivityName attribute in the element.

If your app supports Android 4.0 and lower, include the Support Library with your app and add a element inside the . Then specify the parent activity as the value for android.support.PARENT_ACTIVITY, matching the android:parentActivityName attribute.

因此您需要添加元数据以使其适用于所有设备。添加支持库并尝试这样做:

<activity
android:name=".ArticleView"
android:label="@string/title_activity_article_view"
android:parentActivityName="com.example.android.kheti.ArticleActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.kheti.ArticleActivity" />
</activity>

关于android - 标题栏上的后退按钮不适用于 Lollipop 之前的设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31059511/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com