【问题标题】:Android Fragments: The fragment is not getting displayed. No warnings, No ErrorsAndroid 片段:片段未显示。没有警告,没有错误
【发布时间】:2016-12-18 10:36:52
【问题描述】:

主要活动:

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(我正在尝试用片段的相对布局替换):

<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>

片段不显示。我做错了什么?

【问题讨论】:

  • 将片段放置在容器布局中,例如FrameLayout 而不是RelativeLayout - 将Activity 中的RelativeLayout 替换为FrameLayout
  • 感谢您的回复。
  • 但是,这不起作用..还有其他建议吗??

标签: android android-fragments


【解决方案1】:

我猜你想要做的是让动态片段填充你的主要活动的一部分。为此,您应该在主 XML 布局文件中拥有一个带有 id 的 &lt;FrameLayout&gt; 组件,并在 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();

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    相关资源
    最近更新 更多