android 绘制5格电量,自定义电池(5格电量)

由于用户需求自定义扩展view比较麻烦就是用view布局组合了一个简单的电池

1.colors.xml

#FF0099

#2BB043

2.rectangle_shape.xml

android:shape="rectangle" >

3.activity_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.test.MainActivity" >

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_alignParentRight="true"

android:gravity="center" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_world" />

android:layout_width="30dp"

android:layout_height="15dp"

android:gravity="center"

android:background="@drawable/rectangle_shape"

>

android:id="@+id/view1"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_marginLeft="5dp"

android:layout_marginTop="4dp"

android:layout_marginBottom="4dp"

android:layout_weight="1"

android:background="@color/green"

/>

android:id="@+id/view2"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_marginLeft="1dp"

android:layout_marginTop="4dp"

android:layout_marginBottom="4dp"

android:layout_weight="1"

android:background="@color/green" />

android:id="@+id/view3"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_marginLeft="1dp"

android:layout_marginTop="4dp"

android:layout_marginBottom="4dp"

android:layout_weight="1"

android:background="@color/green" />

android:id="@+id/view4"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_marginLeft="1dp"

android:layout_marginTop="4dp"

android:layout_marginBottom="4dp"

android:layout_weight="1"

android:background="@color/green" />

android:id="@+id/view5"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_marginLeft="1dp"

android:layout_marginRight="5dp"

android:layout_marginTop="4dp"

android:layout_marginBottom="4dp"

android:layout_weight="1"

android:background="@null" />

android:id="@+id/view_head"

android:layout_width="3dp"

android:layout_height="5dp"

android:background="@color/green" />

4.MainActivity.java

package com.example.test;

import java.util.ArrayList;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

public class MainActivity extends Activity {

ArrayListviews;

int[]viewIds={R.id.view1,R.id.view2,R.id.view3,R.id.view4,R.id.view5};

int number=0;//电量

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

setViews();

}

@SuppressWarnings("deprecation")

private void setViews() {

views=new ArrayList();

for (int i = 0; i < viewIds.length; i++) {

views.add(findViewById(viewIds[i]));

}

//初始化电量值

number=2;

//重置电量

for (int i = 0; i < views.size(); i++) {

views.get(i).setBackgroundDrawable(null);

}

if(number==1){

views.get(0).setBackgroundColor(getResources().getColor(R.color.red));

}else{

for (int a = 0; a < number; a++) {

views.get(a).setBackgroundColor(getResources().getColor(R.color.green));

}

}

}

}

最后抽出时间贴上下载地址:http://download.csdn.net/detail/u013042707/8510069

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值