4

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

<Button 
    android:id="@+id/button_one_activity_one"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="First Button"
    />

<fragment
android:name="fragments.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/first_fragment" />    

    <Button 
    android:id="@+id/button_two_activity_one"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Second Button"
    />        
</LinearLayout>

主な活動クラスはこんな感じ

package com.example.testfragmentshoneycomb;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

first_fragment.xmlは次のようになります

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/grey" >"

<TextView
    android:id="@+id/text_view_one_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Text View one" />

<TextView
    android:id="@+id/text_view_two_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Text View two" />

<TextView
    android:id="@+id/text_view_three_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Text View three" />

</LinearLayout>

FirstFragmentクラスはこんな感じです

package fragments;


import com.example.testfragmentshoneycomb.R;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FirstFragment extends Fragment{

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.first_fragment, container, false);
    return view;
}

}

画面には最初のボタンのみが表示され、他には何も表示されません。activity_main.xmlから最初のボタンを削除すると、フラグメントは表示されますが、2番目のボタンは表示されません。

最小SDKバージョンは11で、ビルドターゲットはandroid4.1です。

4

4 に答える 4

3

これは、デフォルトでLinearLayoutの方向がであるためですhorizontal。したがって、画面全体の幅はとでキャプチャされFirst ButtonますFragment

本当に見たいですか?

First_Button              Fragment            Second_Button

はいの場合は使用しますlayout_weight。いいえの場合はorientation=vertical、LinearLayoutに渡して、レイアウト出力を次のように表示します。

First_Button              
Fragment
Second_Button
于 2012-12-28T09:53:39.570 に答える
3

android:orientation="vertical"アクティビティレイアウトで設定します。

于 2012-12-28T09:55:43.800 に答える
1

LinearLayoutの向きを垂直に設定します。デフォルトでは水平です。ドキュメントを注意深く読む

于 2012-12-28T09:55:13.057 に答える
1

次のレイアウトを使用します。

   <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="vertical" >

<Button 
    android:id="@+id/button_one_activity_one"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="First Button"
    />

<fragment
android:name="fragments.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/first_fragment" />    

    <Button 
    android:id="@+id/button_two_activity_one"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Second Button"
    />        
</LinearLayout>
于 2012-12-28T09:58:06.380 に答える