继上一篇仿新浪微博底栏,我们在写个仿QQ空间底栏的效果。
先看主布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="@+id/frame_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/frameLayout1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#ffffff" >
</FrameLayout>
<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/qz_bg_toolbar_new" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="1dp"
android:gravity="center_horizontal" >
<FrameLayout
android:id="@+id/layout_friendfeed"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_weight="1"
android:background="@drawable/home_btn_bg">
<ImageView
android:id="@+id/image_friendfeed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:layout_marginTop="1.0dip"
android:background="@drawable/tab_btn_friendfeed"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="6.0dip"
android:text="动态"
android:textColor="#ffffff"
android:textSize="10sp" />
</FrameLayout>
<FrameLayout
android:id="@+id/layout_myfeed"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_weight="1"
android:background="@drawable/home_btn_bg">
<ImageView
android:id="@+id/image_myfeed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:layout_marginTop="1.0dip"
android:background="@drawable/tab_btn_myfeed"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="6.0dip"
android:text="与我想关"
android:textColor="#ffffff"
android:textSize="10sp" />
</FrameLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</FrameLayout>
<FrameLayout
android:id="@+id/layout_home"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_weight="1"
android:background="@drawable/home_btn_bg">
<ImageView
android:id="@+id/image_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:layout_marginTop="1.0dip"
android:background="@drawable/tab_btn_home"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="6.0dip"
android:text="我的空间"
android:textColor="#ffffff"
android:textSize="10sp" />
</FrameLayout>
<FrameLayout
android:id="@+id/layout_more"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_weight="1"
android:background="@drawable/home_btn_bg">
<ImageView
android:id="@+id/image_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:layout_marginTop="1.0dip"
android:background="@drawable/tab_btn_more"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="6.0dip"
android:text="更多"
android:textColor="#ffffff"
android:textSize="10sp" />
</FrameLayout>
</LinearLayout>
</FrameLayout>
<ImageView
android:id="@+id/toggle_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/qz_bg_toolbar_btn_normal" />
<ImageView
android:id="@+id/plus_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/frameLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="6dip"
android:src="@drawable/qz_icon_toolbar_plus" />
</RelativeLayout>
主MainActivity的代码:
package cn.cl.qzonedemo;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
public class MainActivity extends FragmentActivity implements OnClickListener {
private Main1Fragment page1;
private Main2Fragment page2;
private Main3Fragment page3;
private Main4Fragment page4;
private FrameLayout fl_friendfeed;
private FrameLayout fl_myfeed;
private FrameLayout fl_home;
private FrameLayout fl_more;
private ImageView iv_friendfeed;
private ImageView iv_myfeed;
private ImageView iv_home;
private ImageView iv_more;
private ImageView iv_toggle;
private ImageView iv_plus;
private PopupWindow popWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupView();
clickFriendFeedFl();
}
private void setupView() {
fl_friendfeed = (FrameLayout) findViewById(R.id.layout_friendfeed);
fl_myfeed = (FrameLayout) findViewById(R.id.layout_myfeed);
fl_home = (FrameLayout) findViewById(R.id.layout_home);
fl_more = (FrameLayout) findViewById(R.id.layout_more);
iv_friendfeed = (ImageView) findViewById(R.id.image_friendfeed);
iv_myfeed = (ImageView) findViewById(R.id.image_myfeed);
iv_home = (ImageView) findViewById(R.id.image_home);
iv_more = (ImageView) findViewById(R.id.image_more);
fl_friendfeed.setOnClickListener(this);
fl_myfeed.setOnClickListener(this);
fl_home.setOnClickListener(this);
fl_more.setOnClickListener(this);
iv_toggle = (ImageView) findViewById(R.id.toggle_btn);
iv_toggle.setOnClickListener(this);
iv_plus = (ImageView) findViewById(R.id.plus_btn);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.layout_friendfeed:
clickFriendFeedFl();
break;
case R.id.layout_myfeed:
clickMyFeedFl();
break;
case R.id.layout_home:
clickHomeFl();
break;
case R.id.layout_more:
clickMoreFl();
break;
case R.id.toggle_btn:
clickPlusBtn();
break;
default:
break;
}
}
private void clickFriendFeedFl() {
page1 = new Main1Fragment();
FragmentTransaction fragmentTransaction = this
.getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_content, page1);
fragmentTransaction.commit();
fl_friendfeed.setSelected(true);
iv_friendfeed.setSelected(true);
fl_myfeed.setSelected(false);
iv_myfeed.setSelected(false);
fl_home.setSelected(false);
iv_home.setSelected(false);
fl_more.setSelected(false);
iv_more.setSelected(false);
}
private void clickMyFeedFl() {
page2 = new Main2Fragment();
FragmentTransaction fragmentTransaction = this
.getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_content, page2);
fragmentTransaction.commit();
fl_friendfeed.setSelected(false);
iv_friendfeed.setSelected(false);
fl_myfeed.setSelected(true);
iv_myfeed.setSelected(true);
fl_home.setSelected(false);
iv_home.setSelected(false);
fl_more.setSelected(false);
iv_more.setSelected(false);
}
private void clickHomeFl() {
page3 = new Main3Fragment();
FragmentTransaction fragmentTransaction = this
.getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_content, page3);
fragmentTransaction.commit();
fl_friendfeed.setSelected(false);
iv_friendfeed.setSelected(false);
fl_myfeed.setSelected(false);
iv_myfeed.setSelected(false);
fl_home.setSelected(true);
iv_home.setSelected(true);
fl_more.setSelected(false);
iv_more.setSelected(false);
}
private void clickMoreFl() {
page4 = new Main4Fragment();
FragmentTransaction fragmentTransaction = this
.getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_content, page4);
fragmentTransaction.commit();
fl_friendfeed.setSelected(false);
iv_friendfeed.setSelected(false);
fl_myfeed.setSelected(false);
iv_myfeed.setSelected(false);
fl_home.setSelected(false);
iv_home.setSelected(false);
fl_more.setSelected(true);
iv_more.setSelected(true);
}
private void clickPlusBtn() {
showPopupWindow(iv_toggle);
iv_toggle.setImageResource(R.drawable.qz_bg_skin_toolbar_btn_pressed);
iv_plus.setImageResource(R.drawable.qz_icon_toolbar_plusback);
}
private void restorePlusBtn() {
iv_toggle.setImageResource(R.drawable.qz_bg_toolbar_btn_normal);
iv_plus.setImageResource(R.drawable.qz_icon_toolbar_plus);
}
@SuppressWarnings("deprecation")
private void showPopupWindow(View parent) {
if (popWindow == null) {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.write_tab, null);
popWindow = new PopupWindow(view,
LinearLayout.LayoutParams.MATCH_PARENT, 320);
}
popWindow.setFocusable(true);
popWindow.setOutsideTouchable(true);
popWindow.setBackgroundDrawable(new BitmapDrawable());
popWindow.showAsDropDown(parent, Gravity.CENTER, 0);
popWindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
restorePlusBtn();
}
});
}
}
至此为止,主要代码就写完了,具体的请看demo源码: