Android(Java):自定义控件
<?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);
}
}
更多相关文章
- android full screen
- Android(安卓)闹钟源码
- Android(安卓)Tablayout 的使用
- Android(安卓)自定义ViewPager 实现轮播图
- android density
- scrollView的fillviewport
- android访问网络,下载图片,并设置UI
- Android之Window类简介
- (转) Android的Window类