android自定义LinearLayout和View
2016-07-09 15:04
471 查看
自定义线性布局经常用到:
第一种是在扩展的LinearLayout构造函数中使用Inflater加载一个布局,并从中提取出相关的UI组件进行封装,形成一个独立的控件。在使用该控件时,由于它所有的子元素都是在运行时通过代码动态创建的,所以该控件只能以一个独立控件的形式在Layout文件中声明,例如:
实例:
imagebtn.xml
activity_main.xml
第二种方式是:这个自定义VIEW中的任何控件都不是通过XML文件来定义的,而是在JAVA代码中通过动态生成的,然后再addView()加入到你自定义的View中,
第一种是在扩展的LinearLayout构造函数中使用Inflater加载一个布局,并从中提取出相关的UI组件进行封装,形成一个独立的控件。在使用该控件时,由于它所有的子元素都是在运行时通过代码动态创建的,所以该控件只能以一个独立控件的形式在Layout文件中声明,例如:
public class CustomLayout extends LinearLayout{ public CustomLayout(Context context){ LayoutInflater mInflater = LayoutInflater.from(context); View myView = mInflater.inflate(R.layout.receive, null); addView(myView); } }
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" androidundefinedrientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" androidundefinedrientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> < /LinearLayout>
实例:
imagebtn.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/imageView1" android:layout_width="46dp" android:layout_height="46dp" android:layout_gravity="center_vertical" android:layout_marginRight="10dp" android:src="@drawable/confirm" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="确定" android:textSize="25dp" /> </LinearLayout>MyLinearLayout1.java
package com.hust.customlinearlayout; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MyLinearLayout1 extends LinearLayout { private ImageView imageView; private TextView textView; public MyLinearLayout1(Context context){ super(context); } public MyLinearLayout1(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.imagebtn, this); imageView=(ImageView) findViewById(R.id.imageView1); textView=(TextView)findViewById(R.id.textView1); } public void setImageResource(int resId){ imageView.setImageResource(resId); } public void setTextViewText(String text){ textView.setText(text); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal" > <com.hust.customlinearlayout.MyLinearLayout1 android:id="@+id/btn_right" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" /> <com.hust.customlinearlayout.MyLinearLayout1 android:id="@+id/btn_error" android:layout_marginLeft="5dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="wrap_content" /> </LinearLayout>
package com.hust.customlinearlayout; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; public class MainActivity extends ActionBarActivity { private MyLinearLayout1 myLinearLayout1; private MyLinearLayout1 myLinearLayout2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myLinearLayout1=(MyLinearLayout1) findViewById(R.id.btn_right); myLinearLayout2=(MyLinearLayout1) findViewById(R.id.btn_error); myLinearLayout1.setTextViewText("确定"); myLinearLayout2.setTextViewText("取消"); myLinearLayout1.setImageResource(R.drawable.confirm); myLinearLayout2.setImageResource(R.drawable.cancle); myLinearLayout1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "点击的正确按钮", 1).show(); } }); myLinearLayout2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "点击的错误按钮", 1).show(); } }); } }
第二种方式是:这个自定义VIEW中的任何控件都不是通过XML文件来定义的,而是在JAVA代码中通过动态生成的,然后再addView()加入到你自定义的View中,
private class SpeechView extends LinearLayout { private TextView mTitle; private TextView mDialogue; public SpeechView(Context context, String title, String words) { super(context); this.setOrientation(VERTICAL); // Here we build the child views in code. They could also have // been specified in an XML file. mTitle = new TextView(context); mTitle.setText(title); addView(mTitle, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); mDialogue = new TextView(context); mDialogue.setText(words); addView(mDialogue, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); } /** * Convenience method to set the title of a SpeechView */ public void setTitle(String title) { mTitle.setText(title); } /** * Convenience method to set the dialogue of a SpeechView */ public void setDialogue(String words) { mDialogue.setText(words); } }
相关文章推荐
- Android开发中内存、内部存储、外部存储详解
- Android EditText监听焦点事件
- Android 属性动画,补间动画,xml资源动画详解
- Android 基础 -------Activity
- Android中的自定义view和自定义属性TypedArray的使用
- Android adb 命令备忘
- 漫谈android系统(4)bring up panel
- 漫谈android系统(4)bring up panel
- (OK) Android命令行测试BT,WIFI,Sensor工作状态 — svc
- Android隐藏系统信息栏
- android studio 使用 subversion
- Android 文件夹中所有文件
- Android Studio编译时Plugin is too old, please update to a more recent version...解决办法
- 内存泄漏之分析工具MAT
- android隐藏虚拟键盘的代码参考
- 内存泄漏之activitiy被单例绑定
- Android:xUtils3浅析(二)——数据库模块
- Android 复习6 fragment argument
- Android-androidstudio懒惰开发-必备插件
- 开发Android遇到过的坑