【发布时间】:2016-05-06 05:02:40
【问题描述】:
我在这里的 1 个 XML 文件中有 3 个片段和 2 个框架布局,前 2 个片段是文本,它可以正常显示,但是当我替换具有搜索栏的第三个片段时,它消失了! 但是,如果我先显示第三个片段,则搜索栏显示正常并且 TextView 消失了!这是我的代码:
主活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn3=(Button)findViewById(R.id.btn3);
fragment1= new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(fragment1,R.id.fragment1,"FRG1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(fragment2,R.id.fragment2,"FRG2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(fragment3,R.id.fragment2,"FRG3");
}
});
}
public void replaceFragment(Fragment fragment , int containerResId, String tag){
android.support.v4.app.FragmentTransaction trans = getSupportFragmentManager()
.beginTransaction();
trans.replace(containerResId , fragment , tag);
trans.commit();
getSupportFragmentManager().executePendingTransactions();
}
片段 1:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment1,container,false);
return view;
}
片段 2 和 3 与片段 1 相同。
这是 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:orientation="vertical"
tools:context="com.example.macbook.fragment.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment2"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment3"/>
</LinearLayout>
<FrameLayout
android:id="@+id/fragment1"
android:layout_weight="5"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<FrameLayout
android:id="@+id/fragment2"
android:layout_weight="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"></FrameLayout>
</LinearLayout>
片段3.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:visibility="visible" />
</RelativeLayout>
and this happen when i display fragment 3 which have seekbar
if i display fragment 3 first then any TextView can't display
【问题讨论】:
-
replaceFragment(fragment3,R.id.fragment2,"FRG3");改为 replaceFragment(fragment3,R.id.fragment1,"FRG3");并评论该框架布局
-
你能测试一下这种情况吗:将 FrameLayout(片段 1 和 2)的高度设置为静态大小(例如:200dp),删除 layout_weight 看看会发生什么。
标签: android android-layout android-fragments android-studio