将元素添加到fragment_main.xml不提 [英] Adding elements to fragment_main.xml does noting
问题描述
我添加的EditText和Button标签的fragments_main.xml。
不过,这并不显示一个文本字段和按钮,当我运行应用程序。
然而,当我加入的EditText和按钮activity_main.xml中然后正常工作。请帮忙。我有ADT v22.2.1-833290
这里是activity_main.xml中:
I added EditText and Button tags to the fragments_main.xml. But it doesn't show a text field and button when i run the application. However when i add the EditText and Button to activity_main.xml then it works fine. Please help. I have ADT v22.2.1-833290 Here is the 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:
Here is the 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 code:
And here is the MainActivity Code :
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;
}
}
推荐答案
你在哪里加载fragment_main.xml在code?
Where do you load the fragment_main.xml in your code?
通常情况下,活动应该加载一个片段和片段应加载您刚才提到的XML文件。
Typically, an Activity should load a Fragment and the Fragment should load the xml file that you just mentioned.
在上面的code,你只加载 activity_main.xml中
文件中的 MainActivity
。因此,只有 activity_main.xml中
显示在视图中的内容。
In the above code, you are only loading the activity_main.xml
file in the MainActivity
. Therefore, only the content of activity_main.xml
is displayed in the view.
更新
要加载 fragment_main
XML文件,您可以:
To load the fragment_main
XML file, you can either:
-
从
的setContentView替换code在
到MainActivity
行(R.layout.activity_main);的setContentView(R.layout.fragment_main);
Replace the line of code in
MainActivity
fromsetContentView(R.layout.activity_main);
tosetContentView(R.layout.fragment_main);
或者,创建在延长片段
A类 MainActivity
,让该类加载 fragment_main
XML。
OR, create a class extending Fragment
in the MainActivity
and let that class load the fragment_main
XML.
要做到后者,你可以按照一步一步的指导在这里:片段|添加UI
To do the latter, you can follow the step by step guide here: Fragments | Adding a UI
在本质上,创建一个类:
In essence, Create a class:
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
(片段|添加片段的活动)。
要加载,你可以声明 activity_main.xml中
布局内片段
To load, you can either declare the fragment inside the activity_main.xml
layout
<?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>
或者试试穆斯塔法给出了答案。
Or try the answer mustafa gave.
我希望这有助于。
这篇关于将元素添加到fragment_main.xml不提的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!