自定义控件的形式与使用

第一种方式:最常的一种(自定义基本控件)
 1.继承基本控件(View,textView,ImageView等)  如:public class Ios7Arrow extends View{...}
  2.根据自己需求:a.重写相应的方法(重写onDraw()方法很常用)  b.或者加相应的属性(如何加?百度自定义控件很多)
 3.直接在布局文件xml中使用: <org.espier.privchat.widget.Ios7Arrow ......./>

第二种方式:include或直接inflate方式使用(自定义布局控件)
 1.继承布局控件(RelativeLayout,LinnerLayout等)如:public class NavigateBar7 extends RelativeLayout{...}
  2.布局文件父容器使用自定义包名,设计自己的布局:xml文件名为:navigation_bar.xml
  <org.espier.privchat.widget.NavigateBar7 xmlns:android="http://schemas.android.com/apk/res/android"....>...</>
  3.一般重写onFinishInflate()方法,找到要处理的控件,根据自己需求增加或重写其他方法
 4.第一种使用:a.在XML文件中include  b.findViewById()找到控件:
        <include
        android:id="@+id/navigationbar"
        android:layout_alignParentTop="true"
        layout="@layout/navigation_bar" />

        NavigateBar7 navigateBar = (NavigateBar7) view.findViewById(R.id.navigationbar);
  第二种使用: 直接inflate加载控件 NavigateBar7 navigateBar = (NavigateBar) View.inflate(mContext, R.navigation_bar, null);

第三种方式:嵌入布局(非include)方式使用(自定义布局控件)与include方式的差别是将布局文件直接写入到了主布局文件中,这样自定义布局控件的布局就不能重用
 1.继承布局控件(RelativeLayout,LinnerLayout等)如:public class PageIndicatorView extends LinearLayout { ...}
  2.一般重写onFinishInflate()方法,找到要处理的控件,根据自己需求增加或重写其他方法
  3.使用:a.在使用的XML文件直接引用,且将布局写入 b.findViewById()找到控件
        <mobi.espier.notifications.widget.PageIndicatorView
        android:id="@+id/page_indicator_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="10dip"
        android:layout_marginTop="5dip"
        android:background="#00333333"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/page_today"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/elp_notification7_table_today" />

        .......

      </mobi.espier.notifications.widget.PageIndicatorView>

      PageIndicatorView indicatorView = (PageIndicatorView) findViewById(R.id.page_indicator_view);

第四种方式:构造inflate方式使用
 1.继承布局控件(RelativeLayout,LinnerLayout等)如:public class CalendarLayout extends LinearLayout
  2.布局文件父容器使用父类包名,设计自己的布局:xml文件名为:calendar_layout.xml
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"... >...</>
  3. a.在构造方法中inflate方式加载布局

     b.一般重写onFinishInflate()方法,找到要处理的控件,根据自己需求增加或重写其他方法

    如:public CalendarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate(context, R.layout.calendar_layout, this);
    }
 4.使用:a.直接在布局文件xml中使用 b.findViewById()找到控件:

         <mobi.espier.notifications.widget.CalendarLayout
                    android:id="@+id/calendar"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="10dip" />

        CalendarLayout calendarLayout = (CalendarLayout) findViewById(R.id.calendar);




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值