Android自定义控件----例如标题栏(实例示范)

前言:

      Android常用控件没讲,大家看下官网,或者教程学习下即可,接下来给大家示范一个实例Demo,帮助大家更快入门,然后可以具备自学的能力!


正文

1、新建一个项目,并建立一个activity_main.xml

2、写自定义控件:

  第一步:建立一个布局:title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#000"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btnBack"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="Back"
        android:textAllCaps="false" />

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="3"
        android:gravity="center"
        android:text="Title Text"
        android:textColor="#FFF"
        android:textSize="18sp" />

    <Button
        android:id="@+id/btnEdit"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="Edit"
        android:textAllCaps="false" />
</LinearLayout>

在对应的.java文件中写事件:

public class TitleLayout extends LinearLayout {

    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.title, this);
        /*关闭当前窗体*/
        view.findViewById(R.id.btnBack).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ((Activity) getContext()).finish();
            }
        });
        /*点击Edit按钮,响应事件测试*/
        view.findViewById(R.id.btnEdit).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(), "You clicked the edit button!", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

**注意:因为自定义了标题栏,所以需要隐藏掉模拟器自带的标题栏,但由于每个窗体都需要隐藏,所以新建一个类,继承AppCompatActivity,然后让所有需要隐藏标题栏的布局的.java文件都继承该类

public class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
    }
}

3、在最初新建的activity_main.xml中使用该自定义控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.ssts.uiwidgettest.MainActivity">

    <com.example.ssts.uiwidgettest.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></com.example.ssts.uiwidgettest.TitleLayout>

    <Button
        android:id="@+id/btnTest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start Second" />

</LinearLayout>

布局如下:

public class MainActivity extends BaseActivity {

    private EditText editText;
    private ImageView imageView;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        findViewById(R.id.btnTest).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
    }
}

3、测试Back按钮的功能,

新建第三个窗体,并使用自定义控件

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ssts.uiwidgettest.SecondActivity">

<com.example.ssts.uiwidgettest.TitleLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></com.example.ssts.uiwidgettest.TitleLayout>

</RelativeLayout>

对应的.Java文件

public class SecondActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
}

小结:

            感谢您的阅读,如果喜欢,记得给小编点赞哦???????????????????????????????????

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值