Android自定义Layout

在开发Android过程中,有时已有的Loyout布局不能完全满足设计需求,这时需要在已有的Layout基础上上进行扩展,以达到通过配置文件描述布局的设计原则。

首先第一一个布局类,重写其onDraw方法。

Java代码   收藏代码
  1. public static class iRelativeLayout extends RelativeLayout  
  2.     {  
  3.         private Paint   mPaint;  
  4.         private Rect    mRect;  
  5.   
  6.         public iRelativeLayout( Context context, AttributeSet attrs ) {  
  7.             super(context, attrs);  
  8.   
  9.             mRect = new Rect();  
  10.             mPaint = new Paint();  
  11.   
  12.             mPaint.setStyle(Paint.Style.FILL_AND_STROKE);  
  13.             mPaint.setColor(0xFFCBD2D8);  
  14.         }  
  15.   
  16.         @Override  
  17.         protected void onDraw(Canvas canvas)  
  18.         {  
  19.             super.onDraw(canvas);  
  20.   
  21.             canvas.drawColor(0xFFC5CCD4);  
  22.   
  23.             this.getDrawingRect(mRect);  
  24.   
  25.             for (int i = 0; i < mRect.right; i += 7)  
  26.             {  
  27.                 canvas.drawRect(mRect.left + i, mRect.top, mRect.left + i + 2,  
  28.                         mRect.bottom, mPaint);  
  29.             }  
  30.   
  31.         }  
  32.     }  

 其次,在布局配置文件中引用:

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

 

<view xmlns:android="http://schemas.android.com/apk/res/android"

class="iRelativeLayout"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background = "#C5CCD4FF"

>

<LinearLayout

android:id = "@+id/TabLayout_One"

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

android:layout_above = "@+id/Tabs"

>

<ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content">

<RelativeLayout

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

android:visibility = "visible"

>

<Button

android:id = "@+id/ButtonOne"

android:layout_width = "wrap_content"

android:layout_height = "fill_parent"

android:text = "h3h3h3"

/>

<Button

android:id = "@+id/ButtonTwo"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = "11222"

android:layout_below ="@+id/ButtonOne"

/>

<Button

android:id = "@+id/ButtonThree"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = "wass"

android:layout_below ="@+id/ButtonTwo"

/>

</RelativeLayout>

</ScrollView>

</LinearLayout>

</view>

最后,和其他布局一样使用即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值