android中自定义组合组件(一)

1.为什么自定义

在应用开发中,存在许多很相似的格局,我们如果都用类似的代码去挨个实现,难免代码量较多,并且显得不专业。为此,我们通常采用自定义一个组合组件来实现。


2.本文实现自定义后的效果:

在自定义后,下图的界面只需要一个组件即可完成:
这里写图片描述

单击图中任意地方(也就是单击所自定义的组件),效果如下:
这里写图片描述


3.代码:

(闲话不多说,直接上代码)

(1)先写出实现上述界面的布局文件

在res/layout文件夹下新建activity_example.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="10dp"
        android:paddingTop="5dp"
        >
      <TextView 
           android:id="@+id/startGps"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="点击开启GPS"
           android:textColor="#000000"
           android:textSize="20dp"
          />  
        <TextView 
           android:id="@+id/GpsMode"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="GPS已打开"
           android:layout_below="@id/startGps"
           android:textSize="18dp"
           android:textColor="#938192" 
          />  

        <CheckBox 
           android:id="@+id/check"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentRight="true"
           android:gravity="center_vertical"
           android:paddingRight="10dp"
           android:text=""
           android:clickable="false" 
           android:focusable="false"
           android:focusableInTouchMode="false"
           android:checked="true"
            />
    </RelativeLayout>
</RelativeLayout>
(2)接着自定义view,继承一个ViewGroup(这里继承RelativeLayout)

在src目录下新建MyRelativeLayout.java文件,并继承RelativeLayout

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MyRelativeLayout extends RelativeLayout {

    private TextView startGps,GpsMode;
    private CheckBox checkBox;


    //添加RelativeLayout的三个构造方法
    public MyRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        InitView();
    }

    public MyRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        InitView();
    }

    public MyRelativeLayout(Context context) {
        super(context);
        InitView();
    }


    //将布局文件example填充到MyRelativeLayout中
    public void InitView(){
        View.inflate(getContext(), R.layout.activity_example, this);
        startGps=(TextView) findViewById(R.id.startGps);
        GpsMode=(TextView) findViewById(R.id.GpsMode);
        checkBox=(CheckBox) findViewById(R.id.check);
    }

    //设置接口,用于改变格局中的某些组件的值

    public boolean Checked(){
        if(checkBox.isChecked()){
            return true;
        }
        return false;
    }

    public void setGpsMode(String string){
        GpsMode.setText(string);
    }

    public void setCheck(boolean b){
        checkBox.setChecked(b);
    }
}
(3)主页面的布局文件

activity_main.xml代码如下:
(com.person.maomao.MyRelativeLayout(类MyRelativeLayout的全路径)即是之前自定义的布局组件)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >

    <com.person.maomao.MyRelativeLayout
        android:id="@+id/myRelativeLayout"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />

</RelativeLayout>
(4)主页面java代码:

MainActivity.java代码如下:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

    MyRelativeLayout  myRelativeLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myRelativeLayout=(MyRelativeLayout) findViewById(R.id.myRelativeLayout);
        myRelativeLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v){
                // TODO Auto-generated method stub
                if(myRelativeLayout.Checked()){
                    myRelativeLayout.setGpsMode("GPS已关闭");
                    myRelativeLayout.setCheck(false);
                }else{
                    myRelativeLayout.setGpsMode("GPS已打开");
                    myRelativeLayout.setCheck(true);
                }
            }
        });
    }
}
(5)清单文件AndroidManifest.xml:
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

4.结果

之前在效果栏已经展示过,这里就不在上图了。


5.备注

有问题的地方还请各位多多包含,不吝赐教。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值