gpt4 book ai didi

Android fragment : The fragment is not getting displayed. 没有警告,没有错误

转载 作者:行者123 更新时间:2023-11-30 00:50:03 25 4
gpt4 key购买 nike

主要 Activity :

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@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) {
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.nav_home)
{
Toast.makeText(this,"Home",Toast.LENGTH_SHORT).show();
}
else if (id == R.id.nav_chat)
{
Toast.makeText(this,"Chat",Toast.LENGTH_SHORT).show();
ChatFragment chatFragment = new ChatFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.mainlayout_tobe_replaced,chatFragment).commit();
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}

content main xml(我正在尝试替换 Activity 的相对布局):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/mainlayout_tobe_replaced"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.meghana.bluetoothchatapp.MainActivity"
tools:showIn="@layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>

fragment xml(我试图用 fragment 的相对布局替换):

<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"
tools:context="com.example.meghana.bluetoothchatapp.ChatFragment">

<!-- TODO: Update blank fragment layout -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BLUETOOTH"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="127dp"
android:layout_marginTop="211dp"
android:id="@+id/chatfrag_button_bluetooth" />

</RelativeLayout>

fragment 不显示。我做错了什么?

最佳答案

我猜您想要做的是让动态 fragment 填充您的主要 Activity 的一部分。为此,您应该有一个 <FrameLayout>在您的主 XML 布局文件中添加一个带有 id 的组件,并在 replace 方法中使用该 id。

在主布局中

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/mainlayout_tobe_replaced"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.meghana.bluetoothchatapp.MainActivity"
tools:showIn="@layout/app_bar_main">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myDynamicFragment" />
</RelativeLayout>

然后在你的方法中

ChatFragment chatFragment = new ChatFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.myDynamicFragment,chatFragment).commit();

您应该覆盖 fragment 类中的 onCreateView() 方法。阅读文档了解更多详情,希望对您有所帮助。

关于Android fragment : The fragment is not getting displayed. 没有警告,没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41207495/

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