Android 自定义控件
2013-08-15 11:03
197 查看
1)本文简介
创建Android的自定义控件
通过前台、后台添加自定义控件
2)定义一个layout(activity_custom.xml)作为自定义控件的布局代码如下
3) 接下来写一个类(CustomUserControl.java)继承LinearLayout,导入刚刚的布局,并且设置需要的方法,从而使的能在代码中控制这个自定义控件内容的显示。
创建Android的自定义控件
通过前台、后台添加自定义控件
2)定义一个layout(activity_custom.xml)作为自定义控件的布局代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
3) 接下来写一个类(CustomUserControl.java)继承LinearLayout,导入刚刚的布局,并且设置需要的方法,从而使的能在代码中控制这个自定义控件内容的显示。
package com.example.androidapp; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class CustomUserControl extends LinearLayout{ private TextView textView1=null; private Button button1=null; public CustomUserControl(Context context){ super(context); } public CustomUserControl(Context context, AttributeSet attr){ super(context, attr); LayoutInflater layoutInflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); layoutInflater.inflate(R.layout.activity_custom, this); textView1=(TextView)findViewById(R.id.textView1); button1=(Button)findViewById(R.id.button1); } /*设置显示文字*/ public void setTextViewText(String text){ textView1.setText(text); } }4)前台(activity_main.xml)调用自定义控件代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.example.androidapp.CustomUserControl android:id="@+id/customUserControl1" android:layout_width="wrap_content" android:layout_height="wrap_content" > </com.example.androidapp.CustomUserControl> </LinearLayout>5)后台(MainActivity.java)设置前台添加的用户控件和后台添加用户控件并设置代码如下
package com.example.androidapp; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.LinearLayout; public class MainActivity extends Activity { private CustomUserControl customUserControl1=null; private CustomUserControl customUserControl2=null; private LinearLayout linearLayout1=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /*获取前台添加的自定义控件并操作*/ customUserControl1=(CustomUserControl)findViewById(R.id.customUserControl1); customUserControl1.setTextViewText("前台添加自定义控件"); /*后台添加自定义控件并操作*/ linearLayout1=(LinearLayout)findViewById(R.id.linearLayout1); customUserControl2=new CustomUserControl(MainActivity.this, null); customUserControl2.setTextViewText("后台添加自定义控件"); linearLayout1.addView(customUserControl2); } @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; } }
相关文章推荐
- android自定义控件初解
- Android自定义控件(特效三) 自定义View实现图片验证码
- Android 自定义控件之腾讯安全卫士扫描
- Android 自定义控件一(转)
- Android自定义控件——PullZoomView
- Android 实现形态各异的双向侧滑菜单 自定义控件来袭
- Android自定义控件:动画类(七)----属性动画ValueAnimator高级进阶(二)
- 【android自定义控件】android ListView添加侧滑删除
- Android自定义控件实战——水波纹标签云TagCloud
- Android 自定义控件样式 常用属性
- Android自定义控件
- Android 自定义控件打造史上最简单的侧滑菜单01
- android自定义控件Button 带图片文字
- Android 自定义控件打造史上最简单的侧滑菜单02
- Android自定义控件——模拟抛体加速减速效果
- Android 自定义控件PullToZoomListView
- Android控件构架与自定义控件详解(一)Android控件架构与自定义控件绘制原理
- Android自定义控件——仿淘宝、网易、彩票等广告条、Banner的制作
- Android自定义控件之日历控件
- Android自定义控件-(1)