Android实战—微信首页(一)

文章目录

          • 一、项目地址
          • 二、项目目标:
          • 三、项目结构
          • 四、遇到的问题及解决方案

一、项目地址

https://gitee.com/lonelyZhe/Android-wechat/tree/master/

二、项目目标:
  1. wechat首页布局
  2. 点击底部导航栏切换相应内容
    Android实战—微信首页(一)_第1张图片
三、项目结构

Android实战—微信首页(一)_第2张图片

  1. layout布局
      将topbottom单独写成一个”组件“,在activity_main中用include导入即可
      tab01-tab04是用来记录对应Fragment的

  2. MainActiviy中实现逻辑控制
      四个Fragment类是为了绑定layout中的文件

MainActivity.java

package com.lonelyzhe.wechat;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements View.OnClickListener {

    private Fragment mTab01 = new WeixinFragment();
    private Fragment mTab02 = new FrdFragment();
    private Fragment mTab03 = new ContactFragment();
    private Fragment mTab04 = new SettingsFragment();
    private FragmentManager fm;

    private LinearLayout mTabWeixin;
    private LinearLayout mTabFrd;
    private LinearLayout mTabContact;
    private LinearLayout mTabSettings;

    private ImageButton mImgWeixin;
    private ImageButton mImgFrd;
    private ImageButton mImgContact;
    private ImageButton mImgSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initView();  //绑定控件信息
        initFragment();  //添加Fragment信息
        initEvent();  //限定监听区域
        selectFragment(0);  //触发点击事件,默认为0
    }

    //引入Fragment
    private void initFragment(){
        fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.id_content, mTab01);
        transaction.add(R.id.id_content, mTab02);
        transaction.add(R.id.id_content, mTab03);
        transaction.add(R.id.id_content, mTab04);
        transaction.commit();
    }

    private void initView(){
        mTabWeixin = (LinearLayout) findViewById(R.id.id_tab_weixin);
        mTabFrd = (LinearLayout) findViewById(R.id.id_tab_frd);
        mTabContact = (LinearLayout) findViewById(R.id.id_tab_contact);
        mTabSettings = (LinearLayout) findViewById(R.id.id_tab_settings);

        mImgWeixin = (ImageButton) findViewById(R.id.id_tab_weixin_img);
        mImgFrd = (ImageButton) findViewById(R.id.id_tab_frd_img);
        mImgContact = (ImageButton) findViewById(R.id.id_tab_contact_img);
        mImgSettings = (ImageButton) findViewById(R.id.id_tab_settings_img);
    }

    private void initEvent(){
        mTabWeixin.setOnClickListener(this);
        mTabFrd.setOnClickListener(this);
        mTabContact.setOnClickListener(this);
        mTabSettings.setOnClickListener(this);
    }

    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(mTab01);
        transaction.hide(mTab02);
        transaction.hide(mTab03);
        transaction.hide(mTab04);
    }

    private void selectFragment(int i){
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                transaction.show(mTab01);
                mImgWeixin.setImageResource(R.drawable.tab_weixin_press);
                break;
            case 1:
                transaction.show(mTab02);
                mImgFrd.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            case 2:
                transaction.show(mTab03);
                mImgContact.setImageResource(R.drawable.tab_address_pressed);
                break;
            case 3:
                transaction.show(mTab04);
                mImgSettings.setImageResource(R.drawable.tab_settings_pressed);
                break;
        }
        transaction.commit();
    }

    @Override
    public void onClick(View v) {  //监听点击事件,根据控件id响应
        Log.d("onClick", "1");
        resetImgs();
        switch (v.getId()){
            case R.id.id_tab_weixin:
                selectFragment(0);
                break;
            case R.id.id_tab_frd:
                selectFragment(1);
                break;
            case R.id.id_tab_contact:
                selectFragment(2);
                break;
            case R.id.id_tab_settings:
                selectFragment(3);
                break;
        }
    }
    //全部设置成灰色
    public void resetImgs(){
        mImgWeixin.setImageResource(R.drawable.tab_weixin_normal);
        mImgFrd.setImageResource(R.drawable.tab_find_frd_normal);
        mImgContact.setImageResource(R.drawable.tab_address_normal);
        mImgSettings.setImageResource(R.drawable.tab_settings_normal);
    }
}
四、遇到的问题及解决方案

1. requestWindowFeature(Window.FEATURE_NO_TITLE)无效解决方法
Activity的继承问题

public class MainActivity extends AppCompatActivity
//改为
public class MainActivity extends Activity

2. textView文字居中

android:gravity="center"

3. ImageButton的图片问题
 被Andriod Studio的设计器欺骗了,导入ImageButton的图片是src属性才对。那个srcCompat放在设计器里也有效果,但是运行时就显示不出来了!!

在这里插入图片描述

android:src="@drawable/tab_weixin_press" />

在这里插入图片描述

app:srcCompat="@drawable/tab_find_frd_normal" />

4. LinearLayout触发点击事件问题

//LinearLayout中的元素都添加禁止点击事件
android:clickable="false"

你可能感兴趣的:(Android)