美文网首页
Android中Drawable的一些小技巧

Android中Drawable的一些小技巧

作者: casual_v | 来源:发表于2019-10-21 11:50 被阅读0次

1.固定大小

在app开发中,我们很常见的中设计就是文字前面加一个图标,比如


pic1.jpg

但是,UI给我的图标并不能去适配屏幕,得到的结果往往是


pic2.jpg

一个小功能,我是懒得去github找,毕竟还要学习怎么用,自定义一个textview更是费时费力,有时还吃力不讨好...有没有直接xml设定就完事???当然有
自定义一个drawableleft.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
            android:width="20dp"
            android:height="20dp"
            android:drawable="@drawable/ic_launcher_background"></item>
</layer-list>

然后加入textview即可...几乎毫无学习成本

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:drawableLeft="@drawable/drawableleft"
            android:gravity="center"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

2.时间轴

时间轴这个东西,乍一看,感觉没做过怎么办,会不会很难???难道又要github一波了吗???还是自定义走一个???
其实这个也不用,我们将刚刚drawableleft.xml 改一下(懒得建立新文件)上码

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
            android:width="2dp"
            android:gravity="center">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/darker_gray" />
        </shape>
    </item>
    <item
            android:gravity="top|center"
            android:top="10dp">
        <shape android:shape="oval">
            <size
                    android:width="10dp"
                    android:height="10dp" />
            <solid android:color="@color/colorAccent" />
        </shape>
    </item>
</layer-list>

第一个item 是竖线,定义了2dp的宽,还有居中
第二个item就是点,top是指paddingtop的意思


pic3.jpg

在预览上就是这样显示,看着怪异,但是理解就好
然后 我们写一个View 然后用它作为背景

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
        android:orientation="horizontal"
        tools:context=".MainActivity">

    <View
            android:layout_width="20dp"
            android:layout_height="match_parent"
            android:background="@drawable/drawableleft" />

    <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="asdfsfd"

</LinearLayout>
效果是酱紫的 pic4.jpg

当做item用在list上就完了~~

相关文章

网友评论

      本文标题:Android中Drawable的一些小技巧

      本文链接:https://www.haomeiwen.com/subject/mrwzmctx.html