关闭

仿的微信总结

标签: androidimageviewtextviewFragmentimageBUtton
108人阅读 评论(0) 收藏 举报
分类:

看了鸿洋大神做的微信5.1版本的东西,自己模仿着做了一下,现在总结一下。

有相应的代码,可以下载

微信的第一个RelativeLayout

在里面摄者一个LinearLayout宽度设置为fill_parent,高度设置为40dp,里面嵌套一个LinearLayout,这个LinearLayout由一个ImageView,Textview组成




第二个LinearLayout由三个ImageButton组成



微信的第二个LinearLayout

<?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="50dp"
    android:background="#eee">
<LinearLayout
    android:id="@+id/id_ll_chat"
    android:layout_height="47dp"
    android:layout_width="fill_parent"
    android:orientation="horizontal">
    <LinearLayout android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:gravity="center">
        <TextView android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="聊天"
            android:textColor="#000000"
            android:id="@+id/tl"/>
        </LinearLayout>

    <LinearLayout android:layout_height="fill_parent"
                  android:layout_width="fill_parent"
                  android:layout_weight="1"
                  android:orientation="horizontal"
                  android:gravity="center">
        <TextView android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:text="发现"
                  android:textColor="#000000"
            android:id="@+id/tf"/>
        </LinearLayout>
        <LinearLayout android:layout_height="fill_parent"
                      android:layout_width="fill_parent"
                      android:layout_weight="1"
                      android:orientation="horizontal"
                      android:gravity="center">
        <TextView android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:text="通讯录"
                  android:textColor="#000000"
            android:id="@+id/tt"/>
    </LinearLayout>
    </LinearLayout>
    <ImageView
        android:id="@+id/id_iv_tabline"
        android:layout_width="127dp"
        android:layout_height="3dp"
        android:background="@drawable/tabline" />

</LinearLayout>


这个LinearLayout里嵌套了三个LinearLayout,这三个LinearLayout里,每个都有一个TextView,在最后的结尾加一个蓝色的条

下面是主avtivity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <include layout="@layout/shangyi"/>
    <include layout="@layout/shanger"/>
    <android.support.v4.view.ViewPager
        android:id="@+id/id_viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>


在里面加入这两个布局,在加入一个viewpager用于加入下面的可滑动的页面

下面创建几个布局文件

里面只有textView,这几个布局文件的用处就是为fragment提供模板,再分别创建相应的.java文件,

下面我们在MainActivity里进行设置

先要屏蔽actionbar

我们可以用这几个方法

1.getActionbar().hide();

2.或在androidmanifest中设置设置android:theme=""

@style/Theme.AppCompat.Light.NoActionBar

下面是完整代码

package com.example.window7.weixin;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {
    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mDatas;
    private TextView mlTextView;
    private TextView mfTextView;
    private TextView mtTextView;
    private LinearLayout mChatLinearLayout;
    private ImageView mTabline;
    private int mScreen1_3;
    private int mCurrentPageIndex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initTabLine();
        initView();
    }
    public void initTabLine()
    {
        mTabline=(ImageView)findViewById(R.id.id_iv_tabline);
        //shezhi gensuitiao
        Display display=getWindow().getWindowManager().getDefaultDisplay();
        //shezhi display,dedao windoe.manager.defaultdisplay
        DisplayMetrics outMetrics=new DisplayMetrics();
        //shezhi displayMetrics new displayMetrics
        display.getMetrics(outMetrics);
        //dedao getMetrics(outMetrics)
        //huozhe Display display=getWidow().getWindowManager().getDefaultDisplay;
        mScreen1_3 = outMetrics.widthPixels / 3;
        //tede kuanduwei zongpingde 1/3;
        ViewGroup.LayoutParams lp = mTabline.getLayoutParams();
        //dedao layoutParams dexinxi
        lp.width = mScreen1_3;
        //duitajinxing shezhi zhi
        mTabline.setLayoutParams(lp);
    }
    private void initView()
    {
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        // shezhi viewpager
        mlTextView = (TextView) findViewById(R.id.tl);
        //xianglimianjiaru gege textview;
        mfTextView = (TextView) findViewById(R.id.tf);
        //xianglimianjiaru gege textview;
        mtTextView = (TextView) findViewById(R.id.tt);
        //xianglimianjiaru gege textview;
        mChatLinearLayout = (LinearLayout) findViewById(R.id.id_ll_chat);
        //ba linearlayout jiaru

        mDatas = new ArrayList<Fragment>();
        //shezhi yige xinde arrayList;

        YiFragment tab01 = new YiFragment();
        //jiang gege Fragment xinjian;
        ErFragment tab02 = new ErFragment();
        //jiang gege Fragment xinjian;
        SanFragment tab03 = new SanFragment();
        //jiang gege Fragment xinjian;

        mDatas.add(tab01);
        //jiang ge ge fragment jiadao list li
        mDatas.add(tab02);
        //jiang ge ge fragment jiadao list li
        mDatas.add(tab03);
        //jiang ge ge fragment jiadao list li

        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager())
        {
            @Override
            public int getCount()
            {
                return mDatas.size();
            }

            @Override
            public Fragment getItem(int arg0)
            {
                return mDatas.get(arg0);
            }
        };
        mViewPager.setAdapter(mAdapter);
        //viewpager diaoyaong setAdapter(mAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            @Override
            public void onPageSelected(int position)
            {
                resetTextView();
                switch (position)
                {
                    case 0:
                        mlTextView.setTextColor(Color.parseColor("#008000"));
                        break;
                    case 1:
                        mfTextView.setTextColor(Color.parseColor("#008000"));
                        break;
                    case 2:
                        mtTextView.setTextColor(Color.parseColor("#008000"));
                        break;

                }

                mCurrentPageIndex = position;

            }
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPx)
            {
                Log.e("TAG", position + " , " + positionOffset + " , "
                        + positionOffsetPx);

                LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabline
                        .getLayoutParams();

                if (mCurrentPageIndex == 0 && position == 0)// 0->1
                {
                    lp.leftMargin = (int) (positionOffset * mScreen1_3 + mCurrentPageIndex
                            * mScreen1_3);
                } else if (mCurrentPageIndex == 1 && position == 0)// 1->0
                {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_3 + (positionOffset - 1)
                            * mScreen1_3);
                } else if (mCurrentPageIndex == 1 && position == 1) // 1->2
                {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_3 + positionOffset
                            * mScreen1_3);
                } else if (mCurrentPageIndex == 2 && position == 1) // 2->1
                {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_3 + ( positionOffset-1)
                            * mScreen1_3);
                }
                mTabline.setLayoutParams(lp);

            }

            @Override
            public void onPageScrollStateChanged(int arg0)
            {
                // TODO Auto-generated method stub

            }
        });

    }

    protected void resetTextView()
    {
        mlTextView.setTextColor(Color.BLACK);
        mfTextView.setTextColor(Color.BLACK);
        mtTextView.setTextColor(Color.BLACK);
    }

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:380次
    • 积分:42
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    文章分类
    文章存档