1.自定义布局
新建一个title.xml的Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<Button
android:id="@+id/title_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="Back"
android:textColor="#fff"
/>
<TextView
android:id="@+id/title_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="24sp"
android:layout_weight="1"
android:text="Title Text"
android:gravity="center" />
<Button
android:id="@+id/title_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Edit"
android:textColor="#fff"
android:layout_gravity="center"
/>
</LinearLayout>
显示:
【Back】Title Text 【Edit】
在其他Layout中可以使用<include layout="@layout/title" />包含进去
2.自定义控件
第一步:新建控件类
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this); //相当于findViewById,获得title.xml
第二步:
在MainActivity.xml中添加TitleLayout引用
<com.example.test.test.TitleLayout
anroid:layout_width="match_parent"
anroid:layout_height="wrap_content">
</com.example.test.test.TitleLayout>
第三步:
在TitleLayout类中添加按钮点击事件
Button titleBack = (Button) findViewById(R.id.title_back);
Button titleEdit = (Button) findViewById(R.id.title_edit);
titleBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((Activity)getContext()).finish();
}
});
titleEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(),"you clicked Edit button",Toast.LENGTH_LONG).show();
}
});
相关推荐
用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件,用户自定义控件用户自定义控件,用户自定义控件,用户自定义控件,用户自定义...
vb.net 自定义控件 自定义属性 UITypeEditor UI 类型编辑器 实例 提供一个示例 UITypeEditor,它使用 IWindowsFormsEditorService 显示用于用户输入的 Form。 IWindowsFormsEditorService 只能通过 PropertyGrid ...
当QTwidget的控件不满足开发者的需求的时候,如何在QT中快捷的添加自定义控件
C# 自定义控件的属性 添加自定义控件的事件
C#自定义控件库
c#自定义控件实例大全,.NET自定义控件实例大全,WINFORM自定义控件实例大全
C#自定义控件.rar
教程名称: 尚硅谷Android视频教程《自定义控件》Android自定义控件,是中高级程序员必须熟练掌握的技术之一。本套视频涵盖了安卓自定义开发过程中所有的技术问题,课程中讲授的例子全部来源于企业。学习本套视频后...
C#用户自定义控件C#用户自定义控件C#用户自定义控件C#用户自定义控件
自定义控件开发 自定义控件开发 自定义控件开发 自己知道这些了
LABVIEW自定义控件 你可以自己应用在你所需的领域
C# 自定义控件 自定义ComboBox。其他控件的自定义与此类似。
仅供初学者参考学习,Qt5开发的自定义控件 许多精美样式控件 觉得不错的小伙给予我反馈 还有四五十套源码之后慢慢上传分享
C#的ListView添加各种自定义控件,CheckBox,ComboBox等
labveiw自定义控件、以及一些各种控件大约有几百个、下载之后需要进行安装、任何labview版本都可以安装
c# winform自定义控件包括button、进度条、box、提示框、外观等 内含使用实例工程方便使用和上手
C#写的 自定义控件.ppt以及源代码 自定义控件.ppt以及源代码 自定义控件.ppt以及源代码
C# 自定义控件实例 C# 自定义控件实例 C# 自定义控件实例 button,Control,ProgressBar,tabControl, TrackBar
自定义控件(冒泡事件)源码(WebControl) 自定义控件(冒泡事件)源码(WebControl) 自定义控件(冒泡事件)源码(WebControl) 自定义控件(冒泡事件)源码(WebControl)