一个简单androidUI框架的实现

在android开发过程中往往是多个人一起开发一个项目,那么我们如何保持ui设计代码一致性呢,这个当然最好有一个架构设计师先把框架搭出来,后面大家按这个框架来设计才能保证项目及代码的质量,下面是一个简单ui框架用来分享学习。

/**
 * @Description:TitleBaseActivity基类
 * 
 */


public abstract class TitleBaseActivity extends BaseActivity {


private TextView mTitleText;
private TextView mTitleIcon;
private ImageView mBackButton;
private View mLeftTitleContainer;
private ViewStub mRightStub;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_title_base);
ViewStub stub = (ViewStub) findViewById(R.id.title_stub);
View view = stub.inflate();


mTitleText = (TextView) view.findViewById(R.id.title_text);
mTitleIcon = (TextView) view.findViewById(R.id.title_icon);
mBackButton = (ImageView) view.findViewById(R.id.title_back);
mLeftTitleContainer = view.findViewById(R.id.title_container);
mRightStub = (ViewStub) view.findViewById(R.id.stub_right_title);


if (getRightLayout() > 0) {
mRightStub.setLayoutResource(getRightLayout());
View rightView = mRightStub.inflate();
onRightContentCreated(rightView);
}


View contentView = loadContent();
initTitle();
onContentCreate(savedInstanceState, contentView);
}


protected void enableTitle(boolean enabled) {


if (!enabled) {
mLeftTitleContainer.setBackgroundDrawable(null);


} else {
mLeftTitleContainer.setBackgroundResource(R.drawable.action_bar_selected);
}
}


protected void initTitle() {


}


protected void hideTitle() {
mTitleText.setVisibility(View.GONE);
}


private View loadContent() {
int rid = getContentLayout();
if (rid <= 0)
return null;


FrameLayout parent = (FrameLayout) findViewById(R.id.view_content);


LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(rid, null);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);


parent.addView(view, lp);
return view;
}


@Override
protected void onResume() {
super.onResume();
}


protected void setTitleText(CharSequence text) {
mTitleText.setText(text);
}


protected void setTitleText(int textId) {
mTitleText.setText(textId);
}


protected void setTitleIcon(int resId) {
if (resId <= 0) {
mTitleIcon.setVisibility(View.GONE);
return;
}
mBackButton.setImageResource(resId);
mTitleIcon.setVisibility(View.VISIBLE);
}


protected void setTitleIcon(Drawable drawable) {
if (drawable == null) {
mTitleIcon.setVisibility(View.GONE);
return;
}
mBackButton.setImageDrawable(drawable);
mTitleIcon.setVisibility(View.VISIBLE);
}


protected View getLeftTitleContainer() {
return mLeftTitleContainer;
}


protected void enableBack(boolean back) {
if (back) {
mBackButton.setVisibility(View.VISIBLE);
mBackButton.setOnClickListener(backClick);
mTitleIcon.setVisibility(View.GONE);
//mLeftTitleContainer.setOnClickListener(backClick);
} else {
mBackButton.setVisibility(View.GONE);
//mLeftTitleContainer.setOnClickListener(null);
mBackButton.setOnClickListener(null);
mTitleIcon.setOnClickListener(null);


}
}

protected void enableBack(boolean back,OnClickListener listener) {
if (back) {
mBackButton.setVisibility(View.VISIBLE);
mBackButton.setOnClickListener(listener);
mTitleIcon.setVisibility(View.GONE);
// mLeftTitleContainer.setOnClickListener(listener);
} else {
mBackButton.setVisibility(View.GONE);
//mLeftTitleContainer.setOnClickListener(null);
mBackButton.setOnClickListener(null);
mTitleIcon.setOnClickListener(null);


}
}


private View.OnClickListener backClick = new View.OnClickListener() {


@Override
public void onClick(View v) {
finish();
}
};


@Override
protected void onDestroy() {
super.onDestroy();
}


protected abstract void onContentCreate(Bundle savedInstanceState, View content);


protected abstract int getContentLayout();


protected int getRightLayout() {
return 0;
}


protected void onRightContentCreated(View rightView) {


}


protected void setLeftClickListener(OnClickListener listener) {
mTitleIcon.setOnClickListener(listener);
}

protected void setQuantity(String quantityTx) {
mTitleIcon.setText(quantityTx);
}

}


activity_title_base.xml


<?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" >
    
    <ViewStub 
        android:id="@+id/title_stub"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:layout="@layout/title_bar"
        />
    
    <FrameLayout android:id="@+id/view_content" android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        
    </FrameLayout>
    


</LinearLayout>


title_bar.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title_bar"
    style="@style/title" >


    <RelativeLayout
        android:id="@+id/title_container"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:background="@drawable/action_bar_selected"
        android:gravity="left" >


        <ImageView
            android:id="@+id/title_back"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_centerVertical="true"
            android:padding="10dp"
            />


        <TextView
            android:id="@+id/title_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/title_back"
            android:background="@drawable/action_bar_selected"
            android:textColor="@color/title_text_color"
            android:textSize="14sp"
            android:padding="10dp"
            android:drawableLeft="@drawable/btn_shoping_select" />
    </RelativeLayout>


    <TextView
        android:id="@+id/title_text"
        style="@style/title_text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:gravity="center_vertical"
        android:maxWidth="200dp"
        android:singleLine="true"
        android:text="@string/app_name" />


    <ViewStub
        android:id="@+id/stub_right_title"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/action_bar_selected" />


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0.5dp"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/tab_divider2" />


</RelativeLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值