无法在FrameLayout中打开片段

时间:2014-03-31 06:21:08

标签: android android-fragments

Helo每个人,我尝试在FrameLayout中打开片段,但屏幕是白色的。我的代码是:

public class ProfileActivity extends FragmentActivity {
Database db;
TextView tvName, tvCity;
ImageView imageView;
private FragmentTabHost mTabHost;
FrameLayout frameLayout;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    frameLayout = (FrameLayout) findViewById(R.id.frame_layout);
    initialize();
}

private void initialize() {
    tvName = (TextView) findViewById(R.id.tv_name);
    tvCity = (TextView) findViewById(R.id.tv_city);
    db = Database.getInstance(this);
    imageView = (ImageView) findViewById(R.id.iv_profile);
    Fragment fragment = new StatisticFragment();
    FragmentTransaction transaction = getSupportFragmentManager()
            .beginTransaction();
    transaction.replace(R.id.frame_layout, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}
}

activity_profile.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent" android:background="#ffffff">
<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="0dp" android:layout_weight="1">
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="fill_parent" android:layout_weight="1">
        <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/iv_profile" android:src="@drawable/background_profile_first_part"
                android:scaleType="fitXY"/>
    </LinearLayout>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="fill_parent" android:layout_weight="1.4"
            android:background="@drawable/background_profile_second_part">
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/tv_name" android:layout_gravity="right|center_vertical"
                android:textColor="#ffffff"/>
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/tv_city" android:layout_gravity="center"/>
        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <android.support.v4.app.FragmentTabHost
                    android:id="@android:id/tabhost"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    >
                <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:visibility="invisible"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        />
            </android.support.v4.app.FragmentTabHost>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

    <FrameLayout
            android:id="@+id/frame_layout"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="2">
    </FrameLayout>
 </LinearLayout>

StaticFragment.class

public class StatisticFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_statistic, null);
    return v;
}
}

fragment_statistic.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Statistic"
        android:id="@+id/textView" android:layout_gravity="left|center_vertical"      android:textColor="#fff555"/>
   </LinearLayout>

2 个答案:

答案 0 :(得分:0)

将背景应用于统计片段xml

中的线性布局

答案 1 :(得分:0)

I think you have set visibility to invisible inside frame layout so please edit your layout file
from 

<android.support.v4.app.FragmentTabHost
                    android:id="@android:id/tabhost"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    >
                <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:visibility="invisible"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        />
            </android.support.v4.app.FragmentTabHost>


remove this line and try again.

android:visibility="invisible"