UI之自定义View

UI 绘制及自定义view?
---------------------------------------------------

2.1 绘制相关对象


1)Bitmap 对象
2)Canvas 画板
3)Paint 画笔
4).........


2.2 自定义view
1)自定义类的编写
a)直接或间接继承view
b)添加构造方法及重写相关方法(例如onDraw)
public class MyListView extends ListView {

	public MyListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public MyListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MyListView(Context context) {
		super(context);
	}
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		heightMeasureSpec=MeasureSpec.makeMeasureSpec(
		Integer.MAX_VALUE>>2,//此view高度的最大值
		MeasureSpec.AT_MOST);//扩展模式
		super.onMeasure(widthMeasureSpec,
		heightMeasureSpec);
		Log.i("TAG", "onMeasure");
	}
}


public class MainActivity extends Activity {

	private List<String> list=new ArrayList<String>();
	public MainActivity() {
		for(char i='A';i<='Z';i++){
			list.add(String.valueOf(i));
		}
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ScrollView sView=(ScrollView) findViewById(R.id.scrollId);
		ListView lsv=(ListView) findViewById(R.id.lsvId);
		lsv.setAdapter(new ArrayAdapter<String>(this,
		android.R.layout.simple_list_item_1,list));
	    //平滑滚动到顶点位置。
		sView.smoothScrollTo(0, 0);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

-<ScrollView tools:context=".MainActivity" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/scrollId" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">


-<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical">

<ImageView android:layout_height="150dp" android:layout_width="match_parent" android:id="@+id/imgId" android:scaleType="fitXY" android:src="@drawable/a"/>

<包名.MyListView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/lsvId"/>

</LinearLayout>

</ScrollView>




2)属性(Attribute)的自定义(可选)


a)定义属性文件:res/values/attrs.xml
b)属性文件声明属性
<resources>
    <declare-styleable name="CircleView">
        <attr name="radius" format="integer" />
    </declare-styleable>
</resources>
c)在布局文件中给属性赋值(首先要定义命名空间)
d)在自定义View类的构造方法中获得布局文件中定义的属性值
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值