自定义视图view

25 篇文章 0 订阅

转载请注明出处:http://blog.csdn.net/htwhtw123/article/details/68485780
常常开发Android的APP时,大家经常遇到需要重复使用相同View的,而这些View只有一些细微的不同,比如上面的字不同,下面是仿照Android源码中定义View的方式,编写的一个自定义View,并在主活动的Layout中加载它,与之类似的可以在其他活动的布局中加载它。
以下为自定义标题栏举例(标题栏文字可在Activity里设置)(与之类似的,可以在这个标题栏添加按钮等控件,然后和这个TextView一样,在Activity中获取button对象,并对点击事件进行监听)

这里写图片描述
1.编写自己的布局,最外层使用任意的布局,不一定是ConstraintLayout,可以是LinearLayout等:
新建布局文件title:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorAccent">

    <Button
        android:id="@+id/left_bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左按钮"
        android:layout_marginLeft="2dp"

        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintBottom_toTopOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.0"/>

    <TextView
        android:id="@+id/title_tv"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="标题"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintVertical_bias="0.0"
        android:layout_marginBottom="8dp"/>

    <Button
        android:id="@+id/right_bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右按钮"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginRight="16dp"
        app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>

2.建立自己的视图类,要求继承前面自定义布局最外层的类,定义内部空间变量,并获得对象,以供调用视图时直接访问,而如果是公共的属性,可以在这里直接定义,比如这里的左部按钮的退出事件 :

public class TitleView extends ConstraintLayout {

    Button rightBt;
    Button leftBt;
    TextView titleText;

    public TitleView(final Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View titleView = LayoutInflater.from(context).inflate(R.layout.title_view, this);
        leftBt = titleView.findViewById(R.id.left_bt);
        leftBt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "退出", Toast.LENGTH_SHORT).show();
                ((Activity)context).finish();
            }
        });

        rightBt=titleView.findViewById(R.id.right_bt);
        titleText=titleView.findViewById(R.id.title_tv);
    }


}

3.加载布局:
在想要引入控件的Activity的布局文件中加入如下代码,其中“com.example.mylayout”为包名(不可省略):

<com.example.mylayout.TitleView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ></com.example.asus.growvegetables.TitleLayout>

4.想要添加控件的Activity中设置显示文字,获取自定义视图的对象,及其内部控件,对其进行定义:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TitleView titleView= (TitleView) findViewById(R.id.title_view);
        titleView.rightBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "右边", Toast.LENGTH_SHORT).show();
            }
        });
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值