手动创建其他各个布局的java类
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import fragments.Sy;
import fragments.Wd;
import fragments.Wtt;
import fragments.Xg;
public class MainActivity extends AppCompatActivity {
private TextView shouye;
private TextView xigua;
private TextView wtt;
private TextView wode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
shouye = (TextView) findViewById(R.id.shouye);
xigua = (TextView) findViewById(R.id.xigua);
wtt = (TextView) findViewById(R.id.wtt);
wode = (TextView) findViewById(R.id.wode);
//展示第一个视图
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fl, new Sy());
transaction.commit();
shouye.setTextColor(Color.RED);
xigua.setTextColor(Color.GRAY);
wtt.setTextColor(Color.GRAY);
wode.setTextColor(Color.GRAY);
//监听事件
shouye.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fl, new Sy());
transaction.commit();
shouye.setTextColor(Color.RED);
xigua.setTextColor(Color.GRAY);
wtt.setTextColor(Color.GRAY);
wode.setTextColor(Color.GRAY);
}
});
xigua.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fl, new Xg());
transaction.commit();
shouye.setTextColor(Color.GRAY);
xigua.setTextColor(Color.RED);
wtt.setTextColor(Color.GRAY);
wode.setTextColor(Color.GRAY);
}
});
wtt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fl, new Wtt());
transaction.commit();
shouye.setTextColor(Color.GRAY);
xigua.setTextColor(Color.GRAY);
wtt.setTextColor(Color.RED);
wode.setTextColor(Color.GRAY);
}
});
wode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fl, new Wd());
transaction.commit();
shouye.setTextColor(Color.GRAY);
xigua.setTextColor(Color.GRAY);
wtt.setTextColor(Color.GRAY);
wode.setTextColor(Color.RED);
}
});
}
}
帧布局的布局文件
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fl">
</FrameLayout>