gpt4 book ai didi

java - 为什么我的工具栏没有显示任何内容,即使它显示并放置正确?

转载 作者:行者123 更新时间:2023-12-01 09:17:58 25 4
gpt4 key购买 nike

我试图在我的 SettingsActivity 上添加一个 Toolbar,它显示正确,但由于某种原因,我根本无法让它填充其内容。它只是以空白状态出现。这是我的代码:

SettingsActivity.java:

public class SettingsActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_settings, menu);
return true;
}

@Override
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) {
Intent i = new Intent(this, SettingsActivity.class);
startActivity(i);
}

return super.onOptionsItemSelected(item);
}

public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
final View rootview = inflater.inflate(R.layout.activity_settings, container, false);
return rootview;
}
}
}

activity_settings.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="pt.ismai.a26800.readr.activities.SettingsActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_settings" />

</LinearLayout>

</android.support.design.widget.CoordinatorLayout>

content_settings.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/settings"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>

</LinearLayout>

menu_settings.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="pt.ismai.a26800.readr.activities.SettingsActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>

AndroidManifest.xml:

<activity
android:name=".activities.SettingsActivity"
android:label="@string/action_settings"
android:theme="@style/AppTheme.NoActionBar" />

最佳答案

这是由于操作顺序造成的。您当前正在设置工具栏,然后加载包含工具栏的 fragment 。这意味着当您调用 setSupportActionBar(toolbar); 时,工具栏当前将为空。

您可以通过调用以下命令从 SettingsFragment 中设置工具栏来解决此问题:

final View rootview = inflater.inflate(R.layout.activity_settings, container, false);
Toolbar toolbar = (Toolbar) rootview.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

这会获取 fragment 父 Activity 并相应地设置工具栏。

关于java - 为什么我的工具栏没有显示任何内容,即使它显示并放置正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40392754/

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