android#嵌入式布局并创建自定义控件

一、如何在android中嵌入布局文件:

新建一个布局title.xml,该文件为公共文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bg" >
<Button
android:id="@+id/title_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:background="@drawable/back_bg"
android:text="Back"
android:textColor="#fff" />
<TextView
android:id="@+id/title_text"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Title Text"
android:textColor="#fff"
android:textSize="24sp" />
<Button
android:id="@+id/title_edit"
android:layout_width="wrap_content"
第一行代码——Android
128
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:background="@drawable/edit_bg"
android:text="Edit"
android:textColor="#fff" />
</LinearLayout>

我们在主页面中添加如下代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/title" />
</LinearLayout>

 

即可引入公共布局文件

 

二、如何创建自定义控件

在以上代码的基础上新建如下代码:

public class TitleLayout extends LinearLayout {
    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.title, this);

    }
}

 并将主页面代码修改为

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.uicustomviews.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
></com.example.uicustomviews.TitleLayout>
</LinearLayout>

将自定义控件的代码进行修改

public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
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_SHORT).show();
}
});
}
}

这样,在每次引入自定义控件自后,对应的按钮事件绑定就已经完成,省去编写重复代码。

转载于:https://www.cnblogs.com/changwei0708/p/4430018.html

### 回答1: 易语言是一种编程语言,它通俗易懂、简单易学,是初学者学习编程的一种好选择。在易语言中,有Android元素控件模块,它可以让我们轻松地在Android应用程序中添加各种控件,如按钮、文本框、进度条、图片等。 首先,在易语言中添加Android元素控件模块需要先安装Android SDK,并将相关路径配置好,这样易语言才能够调用Android SDK提供的各类控件。 其次,在Android元素控件模块中我们可以找到各种控件的相应调用函数,并进行相应的设置,例如设置按钮的文本内容、大小、字体、颜色等。控件的位置可以通过设置相应的坐标进行控制;控件的点击事件也可以根据需求进行设置,让程序在用户点击控件时做出相应的反应。 总之,易语言的Android元素控件模块为我们的Android应用开发提供了极大的便利,它让初学者也能够轻松地制作出各种漂亮的Android应用程序,同时也为开发者提供了丰富的控件选择,可适应广泛的开发需求。 ### 回答2: 易语言 Android 元素控件模块是易语言编程语言针对 Android 平台开发的一种控件模块,它提供了一系列常见的组件,如文本框、按钮、进度条、图像按钮等。开发者可以使用这些组件快速构建出复杂的 Android 界面。 在易语言 Android 元素控件模块中,每个组件都具备自己的特性,可以自由配置。例如:文本框组件可以自由指定文本内容、字体颜色、字体大小等;按钮组件可以自定义按钮的文本、颜色、大小等;进度条组件可以自定义进度条的样式和颜色等。 易语言 Android 元素控件模块对于 Android 开发者来说非常友好,缩短了开发周期,提高了开发效率。使用易语言 Android 元素控件模块可以使开发者快速完成 Android 应用的开发。 总之,易语言 Android 元素控件模块是易语言编程语言针对 Android 平台设计的一种控件模块,为开发者提供了一系列常用的组件,提高了开发效率,降低了开发成本。 ### 回答3: 易语言android元素控件模块是易语言编程语言中的一个模块,主要用于在android应用程序中使用各种控件元素。易语言是VB语言的一个变体,易语言android元素控件模块提供了一种简单易用的方式,通过控件元素来创建android应用领域的GUI(图形用户界面)。这个模块包括了许多控件元素,比如文本框、按钮、菜单等等,这些元素可以被直接使用或者可以通过模板来创建自定义的控件。 这个模块支持简单的布局和设计,用户可以通过拖拽控件元素或者使用代码来实现,并且易语言android元素控件模块也包括了一些构建应用程序所必须的基本控件,例如文本框、标签和按钮等等,这些都可以通过拖拽添加到应用程序中。同时,易语言android元素控件模块还支持使用事件来响应用户的交互,这些事件包括按钮点击事件、文本框输入事件等等。 总体而言,易语言android元素控件模块是一个非常实用的模块,它能够方便地创建各种控件元素、布局和设计,同时也支持基本事件响应,供开发者使用。它是易语言一个非常重要的模块,它能让易语言成为Android应用程序开发的一个选项,而不仅仅局限在桌面程序或嵌入式编程领域。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值