关于帧布局

手动创建其他各个布局的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>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值