Android(Java):自定义控件

本文详细介绍了如何设计并实现一个自定义的导航栏组件,包括标题样式、背景颜色、按钮布局等关键元素,旨在提供一个灵活且美观的导航栏解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?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);
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值