Android 引入布局

一、仅引入布局文件

1、 新建一个xml布局文件title.xml,写入自定义布局代码

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

    <ImageButton
        android:id="@+id/butBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/back"></ImageButton>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="POWER MANAGER"
        android:layout_gravity ="center"
        android:gravity="left"
        android:layout_weight="2"
        android:layout_margin="5dp"></TextView>

    <ImageButton
        android:id="@+id/butInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/info"></ImageButton>

</LinearLayout>

2、在我们要引入title的布局文件中包含title,使用include

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
    tools:context=".MainActivity">

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


</androidx.constraintlayout.widget.ConstraintLayout>

         这一种方法仅仅把ui引入了,但是ui里控件的功能还需要引入一次写一次,第二种方法把这个功能也封装起来。

二、把控件功能也封装起来

1、还是要写布局文件,同方法一的1

2、新建java类TitleLayout 继承 LinearLayout

public class TitleLayout extends LinearLayout {
    public TitleLayout(Context contex, AttributeSet attrs)
    {
        super(contex,attrs);
        LayoutInflater.from(contex).inflate(R.layout.title,this);

        ImageButton butBack=findViewById(R.id.butBack);      //返回图标按钮
        ImageButton butInfo=findViewById(R.id.butInfo);      //详细信息按钮
        butBack.setBackgroundResource(0);                    //隐藏背景
        butInfo.setBackgroundResource(0);
        //返回按钮
        butBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(contex, "click:back", Toast.LENGTH_SHORT).show();
            }
        });
        //详细信息按钮
        butInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(contex, "click:info", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

3、在我们要引入title的布局文件中,像普通控件一样,不能再用include

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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


</androidx.constraintlayout.widget.ConstraintLayout>

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android网格布局是一种灵活的布局方式,可以用于创建复杂的界面。它通过使用GridLayout布局来实现,该布局Android 4.0及以上版本引入。与TableLayout相比,GridLayout具有更多的功能和更好的使用体验。它的最大特点是可以让组件自动占据整个网格区域,并且每个组件的大小相同,不能改变组件的大小,只能改变组件之间的水平和垂直间隔。\[2\] 在GridLayout中,可以使用一些属性来控制子元素的位置和占据的行列数。例如,可以使用android:layout_column属性来指定子元素在第几列显示,使用android:layout_row属性来指定子元素在第几行显示。如果想要让子元素横跨多列或多行,可以使用android:layout_columnSpan属性和android:layout_rowSpan属性来指定子元素占据的列数和行数。如果想要让子元素填满横跨的行或列,可以使用android:layout_gravity属性,并将其设置为"fill"。\[1\]\[3\] 总之,Android网格布局是一种强大且灵活的布局方式,可以用于创建复杂的界面。通过使用GridLayout布局和一些属性,可以轻松控制子元素的位置和占据的行列数,实现各种布局需求。 #### 引用[.reference_title] - *1* *2* [Android 基础知识4-2.10 GridLayout(网格布局)详解](https://blog.csdn.net/yyxhzdm/article/details/129133470)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Android应用开发之GridLayout(网格布局)](https://blog.csdn.net/u012739527/article/details/123894081)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

small_planet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值