gpt4 book ai didi

android - 使用 Android 5.0 工具栏小部件的 fragment 未显示在 ActionBarActivity 中

转载 作者:太空狗 更新时间:2023-10-29 15:52:57 25 4
gpt4 key购买 nike

当我运行我的应用程序时,MainFragment 根本没有出现在扩展 ActionBarActivity 的 Activity 中。我检查了this post ,但这并没有帮助我解决我的问题。这是我的 Activity 和 fragment :

activity_main.xml:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment())
.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.main, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

fragment_main.xml:

public class MainFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}

我认为我不需要在此处引用任何 xml...一切正常,直到我扩展 ActionBarActivity 而不是 activity_main.xml 中的 Activity。

编辑:添加 xml 以防万一:

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >

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

</LinearLayout>

fragment_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">

// There are several text views and buttons I will not add to this code,
// but just know that this fragment isn't blank.

</RelativeLayout>

编辑 2:我做了一些测试,每当我从 activity_main.xml 中删除工具栏时, fragment 就会出现,并且没有操作栏(因为这是我为我的应用程序设置的主题,以便使用工具栏)。所以这也是我的 toolbar.xml 的 xml,我在 activity_main.xml 中使用它。也许我做错了什么,它隐藏了我的整个 fragment ?

tl;dr: fragment 显示何时从 activity_main.xml 中删除了这行代码:

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

还有这个在 activity_main.xml 中:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

最佳答案

刚开了一个bug report at AOSP .我现在找到并使用的解决方案是在 post() 中调用 add()

new Handler().post(new Runnable() {
@Override
public void run() {
getFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment())
.commit();
}
});

关于android - 使用 Android 5.0 工具栏小部件的 fragment 未显示在 ActionBarActivity 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26727488/

25 4 0
文章推荐: Android 退出自己的 Launcher App
文章推荐: html - 在 Internet Explorer 中设置 `