【问题标题】:Adding elements to fragment_main.xml does noting向 fragment_main.xml 添加元素
【发布时间】:2014-07-04 19:26:22
【问题描述】:

我在 Fragments_main.xml 中添加了 EditText 和 Button 标签。 但是当我运行应用程序时它不显示文本字段和按钮。 但是,当我将 EditText 和 Button 添加到 activity_main.xml 时,它工作正常。请帮忙。我有 ADT v22.2.1-833290 这是activity_main.xml:

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
</LinearLayout>

这里是fragment_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal" >

<EditText android:layout_weight="1"
    android:id="@+id/edit_message"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />
 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send" />

</LinearLayout>

这里是 MainActivity 代码:

public class MainActivity extends Activity {

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

@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;
}

}

【问题讨论】:

  • 请发布您的activity_main.xml 文件和MainActivity 的代码
  • 您似乎没有在任何地方使用您的片段。
  • MainActivity 中仅加载了activity_main.xml。因此视图中只显示activity_main.xml的内容。

标签: android android-fragments


【解决方案1】:

您在代码中的何处加载 fragment_main.xml?

通常情况下,Activity 应该加载一个 Fragment,而 Fragment 应该加载您刚才提到的 xml 文件。

在上面的代码中,您只是加载了MainActivity 中的activity_main.xml 文件。因此视图中只显示activity_main.xml的内容。

更新

要加载fragment_main XML 文件,您可以:

  • MainActivity中的代码行从setContentView(R.layout.activity_main);替换为setContentView(R.layout.fragment_main);

  • 或者,在MainActivity 中创建一个扩展Fragment 的类,并让该类加载fragment_main XML。

要执行后者,您可以按照此处的分步指南进行操作:Fragments | Adding a UI

本质上,创建一个类:

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

并在您的MainActivity (Fragments | Adding a Fragment to an Activity) 中加载创建的Fragment 类。

要加载,您可以在 activity_main.xml 布局中声明片段

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment android:name="com.example.ui.ExampleFragment"
            android:id="@+id/example_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</LinearLayout>

或者试试mustafa给出的答案。

我希望这会有所帮助。

【讨论】:

  • 那么我该如何加载fragment_main.xml?
  • 我更新了我上面的答案来描述如何加载fragment_main.xml
【解决方案2】:

你可以试试吗?

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout frame = new FrameLayout(this);

        setContentView(frame, new LayoutParams(LayoutParams.MATCH_PARENT,    LayoutParams.MATCH_PARENT));

        if (savedInstanceState == null) {
            Fragment newFragment = new ExampleFragment();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(newFragment).commit();
        }
    }

    @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;
    }
    }

ExampleFragment.java

public class ExampleFragment extends Fragment {
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View view=inflater.inflate(R.layout.fragment_main.xml, container, false);

return view;
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-20
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多