Android fragment_main.xml

时间:2014-09-08 13:39:50

标签: android

我见过的所有初学者教程都使用activity_main.xml来设计布局,并且没有fragment_main.xml。但是,每当我这样做时,我都有两个由eclipse生成的xml布局,一个activity_main和一个fragment_main。 Activity_main为空,而fragment_main包含hello world。我拖了一些元素并将行setContentView(R.layout.activity_main);更改为setContentView(R.layout.fragment_main);。错误消失但应用程序在打开时崩溃在我的设备上。

由于这个原因,我无法遵循任何教程。知道发生了什么事吗?

自动生成的fragment_main.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"
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="com.example.sampel.MainActivity$PlaceholderFragment" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</RelativeLayout>

activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="app_name">testApp</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    </resources>

1 个答案:

答案 0 :(得分:2)

为了制造&#34; hello world&#34;仅使用Activity,设置如下:

<强> RES /布局/ activity_main.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">

<TextView
    android:centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</RelativeLayout>

<强> MainActivity.java:

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle saveInstanceState)
    {
        super(saveInstanceState);
        setContentView(R.layout.activity_main);
    }
}

<强>的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="wholepackagename"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="wholepackagename.activity.MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

<强> RES /值/ strings.xml中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Application name</string>
    <string name="hello_world">Hello world!</string>
</resources>