自定义控件 TitleBar
实现步骤:
1.先写好一个公共的自定义控件布局文件 custom_widget.xml 2.在其它布局文件里引用自定义控件(PublicTitleBar) activity_custom.xml 3.写自定义控件的类,主要是提供一些方法 PublicTitleBar.java 4.在 activity 里面设置自定义控件 文字/点击事件等 MainActivity.java
先写好一个公共的自定义控件布局文件 custom_widget.xml
<RelativeLayout 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" android:background="#FFF000"> <!-- org.aking86.test.userdefinedwidget.MyWidget --> <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/title" tools:context=".MainActivity" /> <Button android:id="@+id/btnBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="@string/back" /> <Button android:id="@+id/btnOK" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/ok" /> </RelativeLayout>
在其它布局文件里引用自定义控件(PublicTitleBar) activity_custom.xml
<RelativeLayout 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" >
<org.aking86.test.userdefinedwidget.PublicTitleBar
android:id="@+id/pub_titlebar"
android:layout_width="fill_parent"
android:layout_height="@dimen/pub_titlebar_height" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/pub_titlebar"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linearLayout1"
android:layout_marginLeft="10dp"
android:layout_marginTop="32dp" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:hint="@string/entry_the_title" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnSetTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/settitle" />
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout2"
android:layout_alignParentTop="true"
android:layout_marginLeft="48dp"
android:layout_marginTop="200dp"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
写自定义控件的类,主要是提供一些方法 PublicTitleBar.java
package org.aking86.test.userdefinedwidget; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; public class PublicTitleBar extends RelativeLayout { private View view; private Button btnBack, btnOK; private TextView tvTitle; Context context; public PublicTitleBar(Context context) { super(context); this.context = context; initLayoutInflater(); } public PublicTitleBar(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; initLayoutInflater(); } public PublicTitleBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; initLayoutInflater(); } private void initLayoutInflater() { LayoutInflater lInflater = (LayoutInflater) getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // view = lInflater.inflate(R.layout.custom_widget, this); initView(); } private void initView() { btnBack = (Button) view.findViewById(R.id.btnBack); btnOK = (Button) view.findViewById(R.id.btnOK); tvTitle = (TextView) view.findViewById(R.id.tvTitle); } public void setTitle (String text) { tvTitle.setText(text); } @Override public void setOnClickListener (OnClickListener lsn) { btnBack.setOnClickListener(lsn); btnOK.setOnClickListener(lsn); tvTitle.setOnClickListener(lsn); } }
在 activity 里面设置自定义控件 文字/点击事件等
/** * 设置 自定义控件 */ private void setPublicTitleBar() { PublicTitleBar titleBar = (PublicTitleBar) findViewById(R.id.pub_titlebar); titleBar.setTitle ("Welcome...."); titleBar.setOnClickListener (new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnBack: Toast.makeText(getBaseContext(), "btnBack", Toast.LENGTH_SHORT).show(); break; case R.id.btnOK: Toast.makeText(getBaseContext(), "btnOK", Toast.LENGTH_SHORT).show(); break; case R.id.tvTitle: Toast.makeText(getBaseContext(), "Title...", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(getBaseContext(), "default...", Toast.LENGTH_SHORT).show(); break; } } }); }