自定义效果:实现:图片和文字混合

首先创建需要组合的子布局:
<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();}});}}

更多相关文章

  1. LinearLayout和RelativeLayout
  2. Android(安卓)Map开发基础知识学习笔记
  3. Android(安卓)平台搭建
  4. android 参数含义
  5. Android(安卓)布局属性大全
  6. Android中有关布局的几个问题
  7. 对话框式Activity的设置
  8. Android(安卓)getWindow().setFlags方法与SD卡权限
  9. Android常用控件

随机推荐

  1. Android 以widget的方式集成Dcloud h5+SD
  2. Android系统升级全流程
  3. Android支持的视频格式
  4. android主流UI布局
  5. [置顶] 超赞!!Android视图讲解!!以及Andr
  6. Android(安卓)Junit单元测试
  7. Android arm linux kernel启动流程(一)
  8. 探究android:largeHeap
  9. Debug Android and Linux suspend and re
  10. Android重要类学习之——Activity