【问题标题】:Can not open fragment in FrameLayout无法在 FrameLayout 中打开片段
【发布时间】:2014-03-31 06:21:07
【问题描述】:

大家好,我尝试在FrameLayout中打开fragment,但是屏幕是白色的。我的代码是:

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>

【问题讨论】:

  • 尝试编辑您的布局 - 将您的 statistic.xml 内容放入 activity.xml 而不是 FrameLayout。我的猜测是你的布局有问题。
  • activity.xml 中没有单个根视图组。尝试在您的顶部 LinearLayout 和 FrameLayout 周围包裹一个 RelativeLayout。
  • @mach,好的。我现在就试试
  • @mach,不,我有根元素

标签: android android-fragments


【解决方案1】:

为统计片段xml中的线性布局应用背景

【讨论】:

  • 我不明白你的意思
  • 将背景颜色或图像应用于您正在膨胀的片段的布局
  • 我有一个黄色的文字在那里
  • 为什么把framelayout的高度设置为0??
  • 因为我设置了重量=2
【解决方案2】:
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"

【讨论】:

  • 这是另一种框架布局,我尝试在 android:id="@+id/frame_layout" 中设置它
猜你喜欢
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多