改变片段时SeekBar会消失吗?

时间:2016-05-06 05:02:41

标签: android android-layout android-fragments android-studio

我在这里的1个XML文件中有3个片段和2个framelayout,前2个片段是文本并且它正常显示,但是当我替换了具有seekbar的第三个片段时它就消失了! 但是,如果我先显示第三个片段,那么搜索栏显示正常,TextView就消失了!这是我的代码:

MainActivity

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

Fragment3.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

1 个答案:

答案 0 :(得分:0)

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

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="10"
        android:orientation="vertical">

    <FrameLayout
        android:id="@+id/fragment1"
        android:layout_weight="5"
        android:layout_width="match_parent"
        android:layout_height="0dp">
    </FrameLayout>


    <FrameLayout
        android:id="@+id/fragment2"
        android:layout_weight="5"
        android:layout_width="match_parent"
        android:layout_height="0dp">
    </FrameLayout>
    </LinearLayout>
</LinearLayout>

2)MainActivity.class

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {

    Fragment1 fragment1;
    Fragment2 fragment2;
    Fragment3 fragment3;
    Button btn1, btn2, btn3;

    @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();
    }
}

3)片段1与片段2和3相同

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class Fragment1 extends Fragment {

    @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;
    }

4)Fragment1.xml与fragment2.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="match_parent"
        android:layout_height="wrap_content"
        android:text="Hi"
        android:textSize="20dp"
        android:layout_margin="20dp"/>

</LinearLayout>

5)fragment3.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>