Android--动态添加控件
来源:互联网 发布:福利ios直播软件 编辑:程序博客网 时间:2024/05/17 04:53
package com.mrzhu.edittest;import android.app.Activity;import android.os.Bundle;import android.view.ViewGroup;import android.widget.RelativeLayout;public class EditTestActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//自定义的带提示文本的EditText组件EditTextTest ed = new EditTextTest(this);//设置从左边140px处开始输入文本ed.setPadding(140, 0, 0, 0);//装载一个布局文件,向这个布局中动态添加一个EditTextRelativeLayout relativeLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.main, null);//创建一个LayoutParams对象RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);//设置android:layout_below属性的值 layoutParams.addRule(RelativeLayout.BELOW, R.id.button2);//动态添加EditTextrelativeLayout.addView(ed, layoutParams);//设置布局setContentView(relativeLayout);}}
自定义的EditText控件:
package com.mrzhu.edittest;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.widget.EditText;public class EditTextTest extends EditText {public EditTextTest(Context context) {super(context);}public EditTextTest(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public EditTextTest(Context context, AttributeSet attrs) {super(context, attrs);}@Overrideprotected void onDraw(Canvas canvas) {Paint paint = new Paint();paint.setTextSize(20);paint.setColor(Color.GRAY);canvas.drawText("输入提示文本:", 10, getHeight() / 2 + 5, paint);super.onDraw(canvas);}}
main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:id="@+id/button2" android:layout_width="80dp" android:layout_height="80dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="150dp" android:onClick="click" android:text="Button" /></RelativeLayout>
- Android 动态添加控件
- Android--动态添加控件
- Android 动态添加控件
- android --动态添加控件
- android 动态添加控件
- Android 动态代码添加控件
- android 动态设置控件的大小 和 动态添加控件
- Android中利用LinearLayout动态添加控件
- Android中利用LinearLayout动态添加控件
- Android中利用LinearLayout动态添加控件
- Android中利用LinearLayout动态添加控件
- Android中利用LinearLayout动态添加控件
- android 在布局中动态添加控件
- Android动态添加控件约束位置
- Android根据布局文件动态添加控件
- android代码中动态添加控件
- android 动态添加控件控制属性问题
- android中怎么动态添加TextView控件
- 远程安装oracle11G出现安装向导后,很慢,无法正常点击下一步配置
- Ajax用法
- android犯错日记3
- POJ-2935-Basic Wall Maze
- 向Makefile传宏
- Android--动态添加控件
- [SSP]HTTP权威指南——Web服务器初探
- 合并两个已经排序的数组
- HDU 3499 Flight 反向建图
- 用Django写的贴吧
- AMR文件格式分析
- ajax验证码
- 关于VC6.0没有”ResourceView”标签的问题
- [Leetcode] Minimum Window Substring