【android自定义控件】LinearLayout定义ActionBar样式
来源:互联网 发布:程序员简历下载 编辑:程序博客网 时间:2024/05/16 00:44
其实大家看到都ActionBar说白了,就是自定义的一个Linearlayout或者RelatedLayout;今天就练练LinearLayout
自定义。
LinearLayout自定义方法有多种:
1、自定义xml布局,然后加载布局,自定义一个View继承LinearLayout
2、在自定义控件中声明它的所有子元素,然后在Layout文件中像使用LinearLayout一样去进行布局,
第二种比较烦 ,它需要在Layout文件中定义好子元素之后,要在代码 onFinishInflate() 进行匹配子元素
我就说说加载布局文件的方法吧。
首先:定义好layout文件
<?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="fill_parent" android:orientation="horizontal" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:paddingBottom="5dip" android:paddingLeft="40dip" android:paddingTop="5dip" android:src="@drawable/right_icon" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="8dip" android:text="主题" android:textColor="#000000" /> <LinearLayout android:layout_width="100dp" android:layout_height="fill_parent" android:orientation="horizontal" > <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:paddingBottom="5dip" android:paddingLeft="12dip" android:paddingTop="5dip" android:src="@drawable/home_icon" /> <ImageView android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:paddingBottom="5dip" android:paddingLeft="12dip" android:paddingTop="5dip" android:src="@drawable/add_icon" /></LinearLayout></LinearLayout>
接下来自定义一个MyLinearLayout继承LinearLayout,并且加载刚刚写好的layout文件。
public class MyLinearLayout extends LinearLayout {private ImageView imageView,iv_home,iv_add;private TextView textView;public MyLinearLayout (Context context) {super(context);// TODO Auto-generated constructor stub}public MyLinearLayout (Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubLayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);inflater.inflate(R.layout.actionbar, this);imageView=(ImageView) findViewById(R.id.imageView1);iv_home=(ImageView) findViewById(R.id.imageView2);iv_add=(ImageView) findViewById(R.id.imageView3);textView=(TextView)findViewById(R.id.textView1);}/** * 设置图片资源 */ public void setImageResource(int resId) { imageView.setImageResource(resId); } /** * 设置显示的文字 */ public void setTextViewText(String text) { textView.setText(text); } }
再者:需要的时候使用定义好的MyLinearLayout控件
<?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="fill_parent" android:orientation="horizontal" > <cn.com.demo.view.MyLinearLayout android:id="@+id/ll_actionbar" android:layout_height="fill_parent<span style="font-family: Tahoma, 'Microsoft Yahei', Simsun;">" </span> android:layout_width="wrap_content" android:background="@drawable/bg" /></LinearLayout>
0 0
- 【android自定义控件】LinearLayout定义ActionBar样式
- android ActionBar自定义样式
- android actionbar 自定义样式
- Android自定义控件---继承Linearlayout
- android:自定义actionbar样式和布局
- android自定义控件样式
- android自定义控件样式
- Android控件样式自定义
- android自定义控件样式
- android自定义控件、样式
- 自定义Actionbar样式
- ActionBar自定义样式
- 自定义ActionBar样式
- 自定义ActionBar文字样式
- 自定义ActionBar的样式
- 自定义actionBar的样式
- 自定义actionbar 样式
- Android开发随记(3)-- ActionBar控件显示、Checkbox样式
- Ubuntu下串口minicom的配置
- Linux性能分析
- delphi XE5下 andriod 广告图片的demo
- 小细节:关于C++各种变量的size问题。
- MySQL基本操作命令
- 【android自定义控件】LinearLayout定义ActionBar样式
- vector
- memcached window版服务器端安装
- CF 439D(251D题)Devu and his Brother
- Unix高级编程 使用setvbuf实现setbuf
- C文件流操作的fread和fseek
- Texture类学习
- Linux性能分析2
- android最全的面试题