由于用户需求自定义扩展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