第一行代码 第三章 引入布局和自定义控件

本文介绍了Android开发中如何引入布局以避免重复代码,以及如何通过自定义控件来处理需要响应事件的场景。动态加载布局使用LayoutInflater的inflate方法,自定义控件则需要指定完整类名。

1、引入布局方式

// 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:layout_height="wrap_content"
    android:background="#000"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btnBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="@drawable/back"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:text="Title Text"
        android:textSize="24sp"
        android:textColor="#fff"/>

    <Button
        android:id="@+id/btnEdit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="@drawable/editor"/>
</LinearLayout>
// 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.sky.uicustomviews.MainActivity">

    <!-- 引入title布局 -->
    <include layout="@layout/title" />

    <Button
        android:id="@+id/btnStartSecondActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="start second activity" />

</LinearLayout>
// second.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_second"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.sky.uicustomviews.SecondActivity">

    <!-- 引入title布局 -->
    <include layout="@layout/title" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:layout_weight="1"
        android:text="Welcome to Second UI"/>
</LinearLayout>
// MainActivity
public class MainActivity extends AppCompatActivity {

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

        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }

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

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

        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }
    }
}

此种方式只是解决了重复编写相同布局代码的问题。如果布局中存在对控件要求能够响应事件,那还是要在每个活动中为这些控件单独编写事件注册的代码,这种情况就需要用自定义控件的方式来解决。

2、自定义控件方式

// TitleLayout继承LinearLayout ,让它成为自定义的标题栏控件
public class TitleLayout extends LinearLayout {

    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.title, this);

        Button btnBack = (Button)findViewById(R.id.btnBack);
        btnBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ((Activity)getContext()).finish();
            }
        });

        Button btnEdit = (Button)findViewById(R.id.btnEdit);
        btnEdit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(), "click edit button", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

动态加载布局方式:LayoutInflater.from(context).inflate(R.layout.title, this);
inflate()接收两个参数:
第一个参数是想要加载的布局文件的id;
第二个参数是给加载好的布局再添加一个父布局,即TitleLayout

// 修改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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.sky.uicustomviews.MainActivity">

    <!-- <include layout="@layout/title" /> -->

    <com.sky.uicustomviews.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btnStartSecondActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="start second activity" />

</LinearLayout>
修改second.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_second"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.sky.uicustomviews.SecondActivity">

    <!-- <include layout="@layout/title" /> -->

    <com.sky.uicustomviews.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:layout_weight="1"
        android:text="Welcome to Second UI"/>
</LinearLayout>

布局中添加自定义控件的方式,需要指明控件的完整类名,即包名不能省略。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值