自定义标题栏遇到的问题

来源:互联网 发布:网络架空地板价格 编辑:程序博客网 时间:2024/05/20 11:25

今天快被自己蠢哭了,研究了一个晚上,在搞标题栏右边的options按钮。
最开始用< Button > 标签,可是不管怎么调整,要么图片会自动加上一个背景,要么图片不在button视图的正中间显示,如下所示:

用background属性,图片被拉伸

用background属性,图片被拉伸

使用src属性,图片被添加上了背景,这时候如果同时使用background属性,那么图片就显示不出来,和layout同色了

使用src属性,图片被添加上了背景 `

drawableXXX,src,background属性都研究了一下,,,最后也算小有收获,最起码学习了drawableStart和drawableEnd等属性,这个的效果如下图:
title_layout
就是左边按钮显示出来的效果,就是图片按钮旁边可以写文字,和QQ一样的效果哈。然后就是右边的options按钮啦,哈哈,改用了< ImageButton >就好啦,然后直接指定src为所需图片就能很好的显示啦,不过后来又遇到一点小问题,就是这样:
optoin按钮没对齐
解决方式就是加上这段代码:

android:layout_alignBottom="@id/button_back"

官方说明:android:layout_alignBottom____Makes the bottom edge of this view match the bottom edge of the given anchor view ID. Accommodates bottom margin.Must be a reference to another resource, in the form “@[+][package:]type:name” or to a theme attribute in the form “?[package:][type:]name”.This corresponds to the global attribute resource symbol layout_alignBottom.

贴上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:id="@+id/title_layout"    android:background="#4b7df1">    <Button        android:id="@+id/button_back"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="#4b7df1"        android:drawableStart="@drawable/back"        android:text="@string/back"        android:textColor="#f1f1f1"/>    <TextView        android:layout_toRightOf="@id/button_back"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/title_text"/>    <ImageButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="#4b7df1"        android:src="@drawable/option"        android:layout_marginEnd="8dp"        android:layout_alignParentEnd="true"        android:layout_alignBottom="@id/button_back"/></RelativeLayout>

最后效果:
这里写图片描述

0 0
原创粉丝点击