android 使用xml定义自己的View
2013-04-01 17:53
453 查看
android 使用xml定义自己的View
运行效果图:
主要activity:
public
class MainActivity extends Activity {
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
主布局文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
<com.example.mydefinedview2.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.example.mydefinedview2.MyView> <-- 此为自定义view -->
</LinearLayout>
自定义view:
public
class MyView extends RelativeLayout{
Context
context;
LayoutInflater
inflate;
View view;
public MyView(Context context) {
super(context);
this.context = context;
initView();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
initView();
}
public
void initView(){
inflate = LayoutInflater.from(context);
view =
inflate.inflate(R.layout.my_view,
null);
addView(view);
}
}
自定义view布局文件:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity"
>
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#f0f"
android:text="这是一个自定义View"
android:gravity="center"
/>
</RelativeLayout>
demo下载地址: http://download.csdn.net/download/lyhdream/5210136
运行效果图:
主要activity:
public
class MainActivity extends Activity {
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
主布局文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
<com.example.mydefinedview2.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.example.mydefinedview2.MyView> <-- 此为自定义view -->
</LinearLayout>
自定义view:
public
class MyView extends RelativeLayout{
Context
context;
LayoutInflater
inflate;
View view;
public MyView(Context context) {
super(context);
this.context = context;
initView();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
initView();
}
public
void initView(){
inflate = LayoutInflater.from(context);
view =
inflate.inflate(R.layout.my_view,
null);
addView(view);
}
}
自定义view布局文件:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity"
>
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#f0f"
android:text="这是一个自定义View"
android:gravity="center"
/>
</RelativeLayout>
demo下载地址: http://download.csdn.net/download/lyhdream/5210136
相关文章推荐
- Android开发使用XML文件定义ImageView的在selected为true和false状态下显示不同的图像
- Android视频播放项目总结之 使用VideoView定义自己的视频播放器
- Android视频播放项目总结之 使用Android中的videoView自己定义,暂停,播放时间,总时间,进度等。
- android 自己定义ViewGroup实现可记载并呈现选择的ListView
- Android 自己定义View须要重写ondraw()等方法
- android中使用自定义控件是报android.view.InflateException: Binary XML 异常
- Android 自己定义RecyclerView 实现真正的Gallery效果
- Android 自己定义ViewGroup手把手教你实现ArcMenu
- android 自己定义标签的使用,实现扁平化UI设计
- Android 自己定义View学习(2)
- Android-自己定义图像资源的使用(1)
- Android使用xml定义带背景Button(解决ImageButton不能加文字的问题)
- Android--xml控件写法和findViewById的对应使用
- 【Android界面实现】信息更新小红点显示——自己定义控件BadgeView的使用介绍
- Android 自己定义TextView 实现文本间距
- Android自己定义实现循环滚轮控件WheelView
- Android开发模板------自己定义SimpleCursorAdapter的使用
- android 自定义属性的使用一、在res/values文件下定义一个attrs.xml文件,代码如下
- 使用tablayout报异常-android.view.InflateException: Binary XML file
- Android使用xml中定义的动画效果