赞
踩
熟悉Android Studio基本操作并在虚拟机中实现类微信界面
具体细节要求:
1.门户一共有四个界面
2.每个界面分为三部分,顶部标题,中间内容框,底部功能界面选择框
3.界面可以根据功能界面选择框在四个界面进行切换,同时会更新中间内容
门户界面顶端app标题,中间为内容框,底部有消息,朋友,通信录,设置四个界面可以随意进行切换。选中的页面,底部选择框图标会变亮。
总体使用布局(layouts)和分段(fragment),对控件进行点击监听从而实现根据各个按钮对四个不同页面进行切换。
在layout中创建top.xml 文件实现顶部app标题
<?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="100dp" android:layout_gravity="center" android:background="@color/black" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="90dp" android:gravity="center" android:text="gwl—wechat" android:textColor="@color/white" android:textSize="30sp" /> </LinearLayout>
创建bottom.xml实现门户界面底部选择框
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="100dp" android:background="@color/black" android:orientation="horizontal"> <LinearLayout android:id="@+id/id_01" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="8" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/weixin_02" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="消息" android:textColor="@color/white" android:textSize="30sp" />
同样创建 weixin.xml ,friend.xml,contact.xml,setting.xml 来作为四个不同的中间内容框
以微信消息界面为例展示中间内容框的部分代码
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" tools:context=".weixinFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="30dp" android:gravity="center" android:text="这是一个微信界面" /> </FrameLayout>
创建四个fragment 布局文件对应四个中间内容文件实现界面布局的切换
以微信消息布局为例展示部分代码
package com.example.myworkgwl01; import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class weixinFragment extends Fragment { public weixinFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_weixin, container, false); } }
定义 initFragment(); showfragment(); hideFragment() becomedark() 通过这几个函数实现对布局文件的调用,隐藏,切换等,从而实现中间布局内容的切换以及底部选择框图标的变亮与变暗。
首先定义四个Fragment类对象,然后建立一个FragmentManager 类对象fragmentManager
private Fragment weixinFragment = new weixinFragment();
private Fragment friendFragment = new friendFragment();
private Fragment contactFragment = new contactFragment();
private Fragment settingweixinFragment = new settingweixinFragment();
private FragmentManager fragmentManager;
initFragment()函数用来添加刚才定义好的对象,用来传递中间界面的文字从而可以实现多窗格界面。
hideFragment()函数,用于隐藏四个Fragement,达到隐藏中间内容框的效果避免四个内容框叠加显示
private void initFragment(){
fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.id_content, weixinFragment);
transaction.add(R.id.id_content, contactFragment);
transaction.add(R.id.id_content, friendFragment);
transaction.add(R.id.id_content, settingweixinFragment);
transaction.commit();
}
private void hideFragment(FragmentTransaction transaction){
transaction.hide(weixinFragment);
transaction.hide(friendFragment);
transaction.hide(contactFragment);
transaction.hide(settingweixinFragment);
onclick()对四个控件进行监听
showfragment()函数实现对于单个布局的显示
@Override public void onClick(View v) { becomedark(); switch (v.getId()){ case R.id.id_01: showfragment(0); break; case R.id.id_02: showfragment(1); break; case R.id.id_03: showfragment(2); break; case R.id.id_04: showfragment(3); break; default: break; } } private void showfragment(int i) { FragmentTransaction transaction=fragmentManager.beginTransaction(); hideFragment(transaction); switch (i){ case 0: transaction.show(weixinFragment); IWeixin.setImageResource(R.drawable.weixin_02); break; case 1: transaction.show(friendFragment); IFriend.setImageResource(R.drawable.friend_02); break; case 2: transaction.show(contactFragment); IContact.setImageResource(R.drawable.contact_02); break; case 3: transaction.show(settingweixinFragment); ISetting.setImageResource(R.drawable.setting_02); break; default: break; } transaction.commit(); }
becomedark()函数切换图片达到图标变暗的效果
private void becomedark(){
IWeixin.setImageResource(R.drawable.weixin_01);
IFriend.setImageResource(R.drawable.friend_01);
IContact.setImageResource(R.drawable.contact_01);
ISetting.setImageResource(R.drawable.setting_01);
}
提前将所需要文件复制到drawable文件下
源码链接:
https://gitee.com/long-yanyu/Mywork_gwl_01
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。