比别人多一点执着,你就会创造奇迹。
本讲内容:Bmob之个人资料设置
界面效果
引导页面 注册页面 登陆页面 会话页面
联系人页面 设置页面 设置页面 个人资料页面
修改昵称页面 个人资料页面
一、本讲可以学到那些知识
1、自定义标题栏
2、fragment页面编写
3、Bmob信息处理
4、个人资料编写
5、头像设置编写
1、自定义标题栏编写
下面是HeaderLayout.java自定义头部控件文件:
/**
* 自定义头部控件
*/
public class HeaderLayout extends LinearLayout {
private LayoutInflater mInflater;
private View mHeader;
// HeaderView控件左侧容器
private LinearLayout mLayoutLeftContainer;
// HeaderView控件右侧容器
private LinearLayout mLayoutRightContainer;
// 标题
private TextView mHtvSubTitle;
// 右侧按钮布局
private LinearLayout mLayoutRightImageButtonLayout;
// 右侧按钮
private Button mRightImageButton;
// 右侧按钮监听接口
public onRightImageButtonClickListener mRightImageButtonClickListener;
// 左侧按钮布局
private LinearLayout mLayoutLeftImageButtonLayout;
// 左侧按钮
private ImageButton mLeftImageButton;
// 左侧按钮监听接口
public onLeftImageButtonClickListener mLeftImageButtonClickListener;
// 头部整体样式
public enum HeaderStyle {
DEFAULT_TITLE, TITLE_LIFT_IMAGEBUTTON, TITLE_RIGHT_IMAGEBUTTON, TITLE_DOUBLE_IMAGEBUTTON;
}
/**
* 设置右侧按钮监听接口
*/
public interface onRightImageButtonClickListener {
void onClick();
}
public void setOnRightImageButtonClickListener(
onRightImageButtonClickListener listener) {
this.mRightImageButtonClickListener = listener;
}
/**
* 设置左侧按钮监听接口
*/
public interface onLeftImageButtonClickListener {
void onClick();
}
public void setOnLeftImageButtonClickListener(
onLeftImageButtonClickListener listener) {
this.mLeftImageButtonClickListener = listener;
}
public HeaderLayout(Context context) {
super(context);
init(context);
}
public HeaderLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
/**
* 实现初始化,加载布局文件
*/
public void init(Context context) {
mInflater = LayoutInflater.from(context);
mHeader = mInflater.inflate(R.layout.common_header, null);
addView(mHeader);
initViews();
}
/**
* 初始化控件
*/
private void initViews() {
mLayoutLeftContainer = (LinearLayout) findViewByHeaderId(R.id.header_layout_leftview_container);
// mLayoutMiddleContainer = (LinearLayout)
// findViewByHeaderId(R.id.header_layout_middleview_container);中间部分添加搜索或者其他按钮时可打开
mLayoutRightContainer = (LinearLayout) findViewByHeaderId(R.id.header_layout_rightview_container);
mHtvSubTitle = (TextView) findViewByHeaderId(R.id.header_htv_subtitle);
}
public View findViewByHeaderId(int id) {
return mHeader.findViewById(id);
}
/**
* 设置控件样式
*/
public void setStyle(HeaderStyle hStyle) {
switch (hStyle) {
case DEFAULT_TITLE:
defaultTitle();
break;
case TITLE_LIFT_IMAGEBUTTON:
defaultTitle();
titleLeftImageButton();
break;
case TITLE_RIGHT_IMAGEBUTTON:
defaultTitle();
titleRightImageButton();
break;
case TITLE_DOUBLE_IMAGEBUTTON:
defaultTitle();
titleLeftImageButton();
titleRightImageButton();
break;
}
}
/**
* 默认文字标题
*/
private void defaultTitle() {
mLayoutLeftContainer.removeAllViews();
mLayoutRightContainer.removeAllViews();
}
/**
* 自定义左侧按钮
*/
private void titleLeftImageButton() {
View mleftImageButtonView = mInflater.inflate(
R.layout.common_header_leftbutton, null);
mLayoutLeftContainer.addView(mleftImageButtonView);
mLayoutLeftImageButtonLayout = (LinearLayout) mleftImageButtonView
.findViewById(R.id.header_layout_imagebuttonlayout);
mLeftImageButton = (ImageButton) mleftImageButtonView
.findViewById(R.id.header_leftbutton);
mLayoutLeftImageButtonLayout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (mLeftImageButtonClickListener != null) {
// 回调方法,调用onLeftImageButtonClickListener接口实现类的方法
mLeftImageButtonClickListener.onClick();
}
}
});
}
/**
* 自定义右侧按钮
*/
private void titleRightImageButton() {
View mRightImageButtonView = mInflater.inflate(
R.layout.common_header_rightbutton, null);
mLayoutRightContainer.addView(mRightImageButtonView);
mLayoutRightImageButtonLayout = (LinearLayout) mRightImageButtonView
.findViewById(R.id.header_layout_imagebuttonlayout);
mRightImageButton = (Button) mRightImageButtonView
.findViewById(R.id.header_rightbutton);
mLayoutRightImageButtonLayout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (mRightImageButtonClickListener != null) {
// 回调方法,调用onRightImageButtonClickListener接口实现类的方法
mRightImageButtonClickListener.onClick();
}
}
});
}
/**
* 获取右边按钮
*/
public Button getRightImageButton() {
if (mRightImageButton != null) {
return mRightImageButton;
}
return null;
}
/**
* 设置标题
*
* @param title
*/
public void setDefaultTitle(CharSequence title) {
if (title != null) {
mHtvSubTitle.setText(title);
} else {
mHtvSubTitle.setVisibility(View.GONE);
}
}
/**
* 设置右侧按钮侦听接的实现类,还包括了标题文本、按钮图片
*
* @param title
* @param id
* @param text
* @param listener
*/
public void setTitleAndRightImageButton(CharSequence title, int id,
String text, onRightImageButtonClickListener listener) {
setDefaultTitle(title);
if (mRightImageButton != null) {
mRightImageButton.setWidth(PixelUtil.dp2px(30));
mRightImageButton.setHeight(PixelUtil.dp2px(30));
mRightImageButton.setTextColor(getResources().getColor(
R.color.transparent));
mRightImageButton.setBackgroundResource(id);
setOnRightImageButtonClickListener(listener);
}
mLayoutRightContainer.setVisibility(View.VISIBLE);
}
public void setTitleAndRightImageButton(CharSequence title, int backid,
onRightImageButtonClickListener onRightImageButtonClickListener) {
setDefaultTitle(title);
mLayoutRightContainer.setVisibility(View.VISIBLE);
if (mRightImageButton != null && backid > 0) {
mRightImageButton.setWidth(PixelUtil.dp2px(30));
mRightImageButton.setHeight(PixelUtil.dp2px(30));
mRightImageButton.setTextColor(getResources().getColor(
R.color.transparent));
mRightImageButton.setBackgroundResource(backid);
setOnRightImageButtonClickListener(onRightImageButtonClickListener);
}
}
/**
* 设置左侧按钮侦听接的实现类,还包括了标题文本、按钮图片
*
* @param title
* @param id
* @param listener
*/
public void setTitleAndLeftImageButton(CharSequence title, int id,
onLeftImageButtonClickListener listener) {
setDefaultTitle(title);
if (mLeftImageButton != null && id > 0) {
mLeftImageButton.setImageResource(id);
setOnLeftImageButtonClickListener(listener);
}
mLayoutRightContainer.setVisibility(View.INVISIBLE);
}
}
下面是res/layout/common_header.xml 布局文件:(HeaderLayout自定义头部控件布局文件)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/ActionBar"
android:baselineAligned="false"
android:focusable="true" >
<LinearLayout
android:id="@+id/header_layout_leftview_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center_vertical|left"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="@+id/header_layout_middleview_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/header_htv_subtitle"
style="@style/ActionBarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/header_layout_rightview_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center_vertical|right"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
下面是res/layout/common_header_leftbutton.xml 布局文件:(左边按钮布局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/header_layout_imagebuttonlayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:minWidth="54dip"
android:padding="6dip" >
<ImageButton
android:id="@+id/header_leftbutton"
android:layout_width="30dip"
android:layout_height="30dip"
android:background="#00000000"
android:clickable="false"
android:contentDescription="@string/app_name"
android:focusable="false"
android:scaleType=