<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="NavigationBar">
<attr name="titletext" format="string" />
<attr name="titlecolor" format="reference|dimension" />
<attr name="titlesize" format="dimension"/>
<attr name="backgroundcolor" format="reference|color" />
</declare-styleable>

<dimen name="splitheight">2dip</dimen>
</resources>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cv="http://schemas.android.com/apk/res/mainclient
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.dongao.mainclient.view.NavigationBar
android:id="@+id/navbar"
android:layout_width="match_parent"
android:layout_height="45dip"
cv:backgroundcolor="@color/navbarbackground"
cv:titlecolor="@android:color/black"
cv:titlesize="13.5sp"
cv:titletext="" >
<Button
android:id="@+id/btnNavBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="@drawable/btn_back"/>
<View
android:layout_width="match_parent"
android:layout_height="2dip"
android:layout_alignParentBottom="true"
android:background="@color/dongao_red" />
</com.dongao.mainclient.view.NavigationBar>
</RelativeLayout>

public class NavigationBar extends RelativeLayout {

private Context mContext;
private ImageButton mLeftButton;
private ImageButton mRghtButton;
private TextView mTitleView;
private String mTitleText;

public NavigationBar(Context context) {
super(context);
mContext = context;
}

public NavigationBar(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
this.setWillNotDraw(false);
//初始化控件
mTitleView = new TextView(context);
mLeftButton = new ImageButton(context);
mRghtButton = new ImageButton(context);

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.NavigationBar);
//设置背景
Drawable background = ta.getDrawable(R.styleable.NavigationBar_backgroundcolor);
this.setBackgroundDrawable(background);
//设置标题
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
lp.addRule(RelativeLayout.CENTER_VERTICAL);
mTitleText = ta.getString(R.styleable.NavigationBar_titletext);
mTitleView.setText(mTitleText);

float titleSize = ta.getDimension(R.styleable.NavigationBar_titlesize, 20);
mTitleView.setTextSize(titleSize);

int textColor = ta.getColor(R.styleable.NavigationBar_titlecolor, Color.BLACK);
mTitleView.setTextColor(textColor);

//加入子控件
this.addView(mTitleView, lp);

ta.recycle();
}

/**
* 设置标题
* @param text
*/
public void setTitleText(String text) {
this.mTitleText = text;
this.mTitleView.setText(mTitleText);
}
}

更多相关文章

  1. android full screen
  2. Android(安卓)闹钟源码
  3. Android(安卓)Tablayout 的使用
  4. Android(安卓)自定义ViewPager 实现轮播图
  5. android density
  6. scrollView的fillviewport
  7. android访问网络,下载图片,并设置UI
  8. Android之Window类简介
  9. (转) Android的Window类

随机推荐

  1. Android(安卓)Handler机制详解:在线程中新
  2. 英特尔® Android* USB 驱动程序安装指南
  3. android中刷新Invalidate和postInvalidat
  4. 解析Android中的XML
  5. Android(安卓)学习笔记——利用JNI技术在
  6. Android(安卓)XML解析学习——Dom方式
  7. Android中各种JAVA包的功能描述
  8. 安卓中一些常用的属性小结
  9. Android消息机制
  10. Android(安卓)内存泄露分析