【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
原创粉丝点击