有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我的片段不会显示在我的活动中

我不熟悉安卓开发,也不熟悉这些片段

因此,我创建了6个xml文件,试图将其作为片段导入到活动中,我已经按照Google的说明一步一步地完成了所有操作,但是片段不会显示在我的活动菜单中

活动菜单。xml文件

    <LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:orientation="horizontal"
    安卓:layout_width="fill_parent"
    安卓:layout_height="fill_parent">

    <fragment 安卓:name="com.grig.alexios.pdaapplication.BeerMenuFragment"
        安卓:id="@+id/headlines_fragment"
        安卓:layout_weight="1"
        安卓:layout_width="0dp"
        安卓:layout_height="match_parent" />

    <fragment 安卓:name="com.grig.alexios.pdaapplication.GinMenuFragment"
        安卓:id="@+id/article_fragment"
        安卓:layout_weight="2"
        安卓:layout_width="0dp"
        安卓:layout_height="match_parent" />


</LinearLayout>

月经活跃。阶级

    package com.grig.alexios.pdaapplication;

import 安卓.support.v4.app.FragmentActivity;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;

public class MenuActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
    }
}

编辑

片段示例

金丝分裂

    package com.grig.alexios.pdaapplication;

import 安卓.os.Bundle;
import 安卓.support.v4.app.Fragment;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;

public class GinMenuFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.gin, container, false);
    }
}

共 (2) 个答案

  1. # 2 楼答案

    我已经测试了你的代码,它运行正常

    好吧,这会管用的

    活动菜单。xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    
    <FrameLayout
        android:id="@+id/headlines_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    
    <FrameLayout
        android:id="@+id/article_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />
    

    以及内在的MenuActivity。java

    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test);
            getSupportFragmentManager().beginTransaction().replace(R.id.article_fragment,new FragA(),"article").commit();
            getSupportFragmentManager().beginTransaction().replace(R.id.headlines_fragment,new FragB(),"headlines").commit();
    
        }
    }
    

    最后,可能是支持库版本有问题。 尝试其他版本并清理项目