很多时候,android提供的组件并不能满足我们的需求,于是我们不得不按需求开发自定义控件。

Step 1.

写好自定义控件的内部布局文件。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/white"    android:orientation="horizontal" >    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical"        android:paddingBottom="5dip"        android:paddingLeft="40dip"        android:paddingTop="5dip"        android:src="@drawable/confirm" />    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical"        android:layout_marginLeft="8dip"        android:text="确定"        android:textColor="#000000" /></LinearLayout>

Step 2:

写好自定义的控件类并继承LinearLayout,并写好相关方法,用于控制自定义控件的内容。

package org.hjw.mybutton;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class my extends LinearLayout {    private ImageView image;    private TextView text;    public my(Context context, AttributeSet attrs) {        super(context, attrs);        LayoutInflater ll = (LayoutInflater) context                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);        ll.inflate(R.layout.my_button, this);        image = (ImageView) findViewById(R.id.image);        text = (TextView) findViewById(R.id.text);    }    public void setImage(int Resid) {        image.setImageResource(Resid);    }    public void setText(String ext) {        text.setText(ext);    }}

Step3:

需要使用自定义控件时,只需要在xml文件中加入即可,这里我们在主布局文件中加入。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/black"    android:orientation="horizontal" >    <org.hjw.mybutton.my        android:id="@+id/myButton1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"      android:background="@drawable/bg_button"   />    <org.hjw.mybutton.my        android:id="@+id/myButton2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"     android:background="@drawable/bg_button"     android:layout_marginLeft="5dp">
 </org.hjw.mybutton.my> </LinearLayout>

Step 4:

为了使用户体验效果更好,可以加入背景图片的变化效果。

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/btn_normal"></item>    <item android:state_pressed="true" android:drawable="@drawable/btn_white"></item>    <item android:state_checked="true" android:drawable="@drawable/btn_white"></item>    <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/btn_normal"></item>    </selector>

Step 5:

最后在主Activity中像使用其他控件一样使用此控件。

package org.hjw.mybutton;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {    private my My, My2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        My = (my) findViewById(R.id.myButton1);        My2 = (my) findViewById(R.id.myButton2);        My2.setImage(R.drawable.cancel);        My2.setText("取消");        My.setImage(R.drawable.confirm);        My.setText("确定");        My.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(getApplicationContext(), "You just clicked!", 1)                        .show();            }        });    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }}

Step 6:

我们看看效果吧。


╭︿︿︿╮{/ o o /} ( (oo) ) ︶ ︶︶

更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. Android(安卓)解压中文乱码(压缩包内中文乱码)
  6. Android(安卓)系统信息获取(CPU,RAM,ROM,Battery,SD-card,版本等)
  7. Android(安卓)给图片、文字、控件、布局添加阴影效果(悬浮效果)
  8. Say Hello to Android!!!
  9. android全格式多媒体播放器(二:opencore媒体系统解析)

随机推荐

  1. Android 通知Notification的两种实现方法
  2. ANDROID 【GRIDVIEW】 DETAILED ANNOTATI
  3. Android 打开文件管理器 获取文件的真实
  4. How to Run Android Applications on Ubu
  5. android 随手记 SQLITE代码 直接能用
  6. Android判断当前的Activity
  7. Drawable简单使用
  8. Android下如何卸载和格式化sdcard
  9. android 打电话
  10. android自定义keystore