第一行代码 UI部分笔记(写一个通用的标题栏)

一 四种基本布局

1.线性布局   LinearLayout

2.相对布局  RelativeLayout

3.帧布局      FrameLayout

4.百分比布局  



二 简单自定义view 通过xml布局来写(一个通用的标题栏)

1.标题栏定义的布局样式名为

title_layout

<?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="match_parent"
    android:orientation="horizontal">

    <Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:text="标题" />

    <Button
        android:id="@+id/more"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="更多" />
</LinearLayout>


2.自定义的标题栏文件

public class TitleLayout extends LinearLayout {
    public TitleLayout(Context context) {
        super(context);
    }

    private TitleLayoutSet titlelayoutSet;

    public TitleLayout(final Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.title_layout, this);

    }


    public void intiData(final TitleLayoutSet titleLayoutSet) {
        Button back = (Button) findViewById(R.id.back);
        Button more = (Button) findViewById(R.id.more);
        final TextView title = (TextView) findViewById(R.id.title);
        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) getContext()).finish();
            }
        });
        titleLayoutSet.setBackImage(back);
        titleLayoutSet.setTitleText(title);
        titleLayoutSet.setMoreText(more);
        more.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                titleLayoutSet.setMoreClickListener();
            }
        });
    }


    public interface TitleLayoutSet {
        void setBackImage(Button back);

        void setTitleText(TextView tv);

        void setMoreClickListener();

        void setMoreText(Button btn);
    }

}

3,使用

在activity的布局文件中

<com.lvyequeen.test.day01.myView.TitleLayout
    android:id="@+id/titleSet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

在activity文件中

private void initView() {

    TitleLayout layout = (TitleLayout) findViewById(R.id.titleSet);
    layout.intiData(new TitleLayout.TitleLayoutSet() {
        @Override
        public void setBackImage(Button back) {
            back.setBackgroundResource(R.mipmap.ic_launcher);
        }

        @Override
        public void setTitleText(TextView tv) {
            tv.setText("我的测试标题呀");
        }

        @Override
        public void setMoreClickListener() {
            Toast.makeText(getApplicationContext(), "点击更多了", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void setMoreText(Button btn) {
            btn.setText("加载更多");
        }
    });

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值