天天看点

手机卫士04-自定义显示图片

首先,先和大家说个对不起先,昨天太赶啦,一时没说清楚昨天的内容,其实昨天还有一些ui的知识点还没讲的,而且那个代码也是有一点问题的,所以今天把它补回来

我们先讲一下,昨天忘记说了的内容

手机卫士04-自定义显示图片

其实昨天忘记说了的就是那个自定义标题栏那里啦

自定义标题栏,其实就是把原来的标题栏隐藏掉,然后再自己写一个textview这些的控件,把它放上去的而已,一说就很简单的啦

但我们这里有一个知识点的

那就是自定义图片,其实上面的那个手机防盗这些文字外面的那个框,就是用到啦自定义图片的啦

要自定义图片,也很简单,我们要先在drawable的目录下面,新建一个xml文件

下面是我自己建的那个文字的背景text_background.xml

<font color="#333333"><font face="arial"><?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle" ><!-- 这里指定了我现在要画的是一个矩形 -->

    <stroke

        android:width="1dip"

        android:color="#ff333333" /><!-- 这个就是一个画笔啦,现在指定了画笔的大小,还有颜色 -->

    <corners android:radius="12dip" /><!-- 现在这里指定我这个矩形是圆角的,12是那个圆角的值 -->

    <solid android:color="@color/background" /><!-- 边框颜色 -->

    <padding

        android:bottom="2dip"

        android:left="8dip"

        android:right="8dip"

        android:top="2dip" /><!-- 这个是内距 -->

</shape></font></font>

复制代码

接下来,就可以在我们的main_item,xml的布局文件里面引用它啦

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="150dip"

    android:layout_height="150dip"

    android:gravity="center_horizontal"

    android:orientation="vertical" >

    <imageview

        android:id="@+id/iv_main_icon"

        android:layout_width="120dip"

        android:layout_height="120dip"

        android:scaletype="fitxy"

        android:src="@drawable/app"

        android:contentdescription="@string/hello_world"/>

    <textview

        android:id="@+id/tv_main_name"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_margintop="5dip"

        android:textsize="18sp"

        android:textcolor="@android:color/black"

        android:text="@string/main"

        android:background="@drawable/text_background"/><!-- 引用刚刚定义的图片,像一般图片一样使用就行的啦,很方便的  -->

</linearlayout>

</font></font>

好,说完自定义图片之后,我说一下那个自定义标题栏那里啦,我刚刚说过啦,就是隐藏原来的,那么如何隐藏呢,其实只要在androidmainfest文件里面,声明那个类的时候给它加上一个theme属性就行的啦

<activity

            android:theme="@android:style/theme.notitlebar"

            android:label="@string/main"

            android:name="com.xiaobin.security.ui.mainactivity" />

然后再给我们的布局文件加上一个textview

<?xml version="1.0" encoding="utf-8"?>

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="@android:color/white"

    <linearlayout

        android:layout_width="match_parent"

        android:layout_height="40dip"

        android:background="@drawable/title_background"

        android:gravity="center_vertical|center_horizontal"

        android:orientation="vertical" >

        <textview

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/main"

            android:textcolor="@android:color/white"

            android:textsize="22sp" />

    </linearlayout>

    <gridview

        android:id="@+id/gv_main"

        android:layout_height="match_parent"

        android:numcolumns="2"

        android:verticalspacing="8dip" />

其实在那个背景里面,我也是用到了自定义图片的,各位也可以用一下的,多点用,才能记得嘛 好啦,现在说一下那个最重要的啦,就是我是在那里找到这些的,是怎样知道这些东西的其实这些,在我们的android的文档里面有的

手机卫士04-自定义显示图片
手机卫士04-自定义显示图片

就是这样的啦,其实api文档还有很多东西的,有空的话可以看看,当然我们这个项目还是会参与很多api里面的东西的好啦,今天就到这里啦,各位可以自己玩一玩那个自定义图片的功能,先预告一下明天的内容,明天次会都我们自定义对话框的,还有给手机防盗加个登录密码  

手机卫士04-自定义显示图片

kb, 下载次数: 250)

继续阅读