Android(安卓)之 自定义控件用法介绍
16lz
2021-12-04
自定义效果:实现:图片和文字混合
首先创建需要组合的子布局:
编写代码,为自定义控件设置值:
主布局文件:
主程序入口代码:
首先创建需要组合的子布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/myimage" android:layout_width="30dp" android:layout_height="30dp" /> <TextView android:id="@+id/mytext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" /></LinearLayout>
编写代码,为自定义控件设置值:
package com.example.userdefinedview;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;/** * 自定义 组建的实现 -- * 功能:复合组件 * @author Administrator * */public class MyLinearLayout extends LinearLayout {// 声明对象属性private ImageView myimage;private TextView mytext; public MyLinearLayout(Context context, AttributeSet attrs) {super(context, attrs);LayoutInflater.from(context).inflate(R.layout.my_linearlayout,this,true); //视图容器装载myimage = (ImageView) findViewById(R.id.myimage); // 获取对象mytext = (TextView) findViewById(R.id.mytext);}/** * 设置图片资源 * @param resID 资源ID */public void setImageViewImageResource(int resID){myimage.setImageResource(resID);}/** * 设置文本内容 * @param text 字符信息 */public void setMyTextText(String text){mytext.setText(text);}}
主布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <com.example.userdefinedview.MyLinearLayout android:id="@+id/my1" android:layout_width="100dp" android:layout_height="wrap_content" android:background="#ff00ff" /> <com.example.userdefinedview.MyLinearLayout android:layout_marginLeft="20dp" android:id="@+id/my2" android:layout_width="100dp" android:layout_height="wrap_content" android:background="#ff00ff" /></LinearLayout>
主程序入口代码:
package com.example.userdefinedview;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Toast;public class MainActivity extends Activity {private MyLinearLayout my1;private MyLinearLayout my2;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);my1 = (MyLinearLayout) findViewById(R.id.my1);my2 = (MyLinearLayout) findViewById(R.id.my2);my1.setImageViewImageResource(R.drawable.a1);my1.setMyTextText("确定");my2.setImageViewImageResource(R.drawable.a6);my2.setMyTextText("取消");my1.setOnClickListener(new OnClickListener() {public void onClick(View v) {Toast.makeText(MainActivity.this, "点击了确定",Toast.LENGTH_LONG).show();}});my2.setOnClickListener(new OnClickListener() {public void onClick(View v) {Toast.makeText(MainActivity.this, "点击了取消",Toast.LENGTH_LONG).show();}});}}
更多相关文章
- LinearLayout和RelativeLayout
- Android(安卓)Map开发基础知识学习笔记
- Android(安卓)平台搭建
- android 参数含义
- Android(安卓)布局属性大全
- Android中有关布局的几个问题
- 对话框式Activity的设置
- Android(安卓)getWindow().setFlags方法与SD卡权限
- Android常用控件