当前位置: 移动技术网 > IT编程>移动开发>Android > 安卓开发笔记(二十九):顶部标题栏

安卓开发笔记(二十九):顶部标题栏

2019年04月26日  | 移动技术网IT编程  | 我要评论

侯卫东官场笔记济阳吧,邹平效能监察连心网,唐嫣微电影

首先上图:

实现这个标题栏,我们还需要一个返回的按钮,这里也贴出来。笔者直接将这个简单的标题栏制作成了一个依赖库,放在到github上,方便下次进行调用。

返回按钮如下:

在使用这个按钮的时候需要注意其尺寸的大小一定要小于我们的标题栏。

view_top.xml

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#50e7ab"
    android:padding="10dp">

    <imageview
        android:id="@+id/top_left"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/great2" />

    <textview
        android:id="@+id/top_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerhorizontal="true"
        android:layout_centervertical="true"
        android:text="首页"
        android:textsize="17sp"
        android:textcolor="#ffffff" />

    <textview
        android:id="@+id/top_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        android:textsize="17sp"
        android:textcolor="#ffffff"
        android:layout_centervertical="true"
        android:layout_alignparentright="true" />
</relativelayout>

新建的topview类:

import android.content.context;
import android.util.attributeset;
import android.view.layoutinflater;
import android.widget.imageview;
import android.widget.relativelayout;
import android.widget.textview;

import com.example.lenovo.deeplove2.r;

public class topview extends relativelayout {

    // 返回按钮控件
    private imageview top_left;
    // 标题tv
    private textview top_title;

    private textview top_right;

    public topview(context context) {
        super(context);
    }

    public topview(context context, attributeset attrs) {
        super(context, attrs);
        // 加载布局
        layoutinflater.from(context).inflate(r.layout.view_top, this);
        // 获取控件
        top_left = (imageview) findviewbyid(r.id.top_left);
        top_title = (textview) findviewbyid(r.id.top_title);
        top_right = (textview) findviewbyid(r.id.top_right);
    }


    // 为左侧返回按钮添加自定义点击事件
    public void setonclickleft(onclicklistener listener) {
        top_left.setonclicklistener(listener);
    }

    // 设置标题的方法
    public void settitle(string title) {
        top_title.settext(title);
    }

    // 设置标题的方法
    public void setrighttitle(string title) {
        top_right.settext(title);
    }


}

调用方法:

 <com.example.lenovo.deeplove.topview
        android:id="@+id/top_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

完毕。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网