美文网首页程序员Android技术知识Android开发
你不知道的TextView和LinearLayout

你不知道的TextView和LinearLayout

作者: eiun | 来源:发表于2016-11-01 15:44 被阅读0次

作为Android UI里面最常用的两个控件其实还有很多强大的功能我们还没完全挖掘出来。然而他们真的很好用···

比如下面这个图,要做这个效果,相信大家经常能看到这样的UI设计,所以你第一反应会怎么做

一般

第一反应嘛,左边一个ImageView,中间一个textview,最后再一个ImageView,然后再用个LinearLayout套起来,妥妥的,哦下面还有个分割线,可以有1dp的view叠上去,搞定收工。

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/image_down_selector" //这是点击效果
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:paddingLeft="10dp" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:contentDescription="@null"
                android:src="@mipmap/icon_erCore" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_weight="1"
                android:text="@string/erCore"
                android:textColor="@color/tv_color"
                android:textSize="@dimen/font_size_14" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center_vertical"
                android:layout_marginRight="10dp"
                android:layout_weight="0"
                android:contentDescription="@null"
                android:src="@mipmap/right_in" />
        </LinearLayout>

        <View style="@style/horizontal_line_gray" />  //这是分割线,我写到style里面了,方便复用

呐,代码我都贴出来了,就是这样子,然而这只是一条就那么多代码了,一般好多条呢
而且Android中的嵌套层级越多,你的流畅度就越低,每套一层进去,它加载的时间是几何倍增的,所以这是很不明智的

然后我告诉你,这一条栏用一个textview就好了
不多说上代码

            <TextView
                android:layout_width="match_parent"  
                android:layout_height="40dp"
                android:text="@string/erCore"
                android:drawableLeft="@mipmap/icon_erCore"  //左边图标
                android:drawableRight="@mipmap/right_in"       //右边图标
                android:background="@drawable/image_down_selector"      //这是点击效果                   
                android:gravity="center|left"
                android:textColor="@color/tv_color"
                android:textSize="@dimen/font_size_14"
                android:paddingLeft="10dp"                             //控制好位置
                android:paddingRight="10dp"
                android:drawablePadding="10dp"
                />

就是这么简单明了···

你说这么简单的事干嘛要突突突的套三四个控件去解决呢,是吧

好,搞定一条,接下来还有下划线,这条东东嘛有时候真的很烦,因为IOS里面是自带的,刷一下就自动出来了(本来就是IOS的设计,没办法UI不懂Android,就算懂,现在UI妥妥的都是参照IOS来设计的,Android的就算苦逼),虽然我写了个style,也是一行代码的事,但是这还不够,还有更加高效简单的方法

那就是你们忽略的LinearLayout

这个大家经常用的啦,可是这个呢LinearLayoutCompat

人家谷歌都给你做好的东西就要懂得拿出来用

用法是这样的

        <android.support.v7.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"

            app:divider="@drawable/line"        //重点
            app:showDividers="end|middle|beginning">   //这个也是

先贴代码吧,drawable里的line是这样子的

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="@color/horizontal_line_color" />  //颜色,弄个灰的差不多的就好

    <!-- 分割线的高度 -->
    <size android:height="0.2dp" />   //一般1就好了,我这特细

</shape>

其实LinearLayoutCompat跟LinearLayout一毛一样的用法,但是人家多了几个字就多了这个炫酷吊炸天的功能

解释下,其实好像不用解释也懂了
app:divider="@drawable/line" 这个就是分割线嘛

app:showDividers="end|middle|beginning">这个就是分割线要出现的位置,不过要注意的是end和beginning不是里面每条item都有分割线,它表示整个layout里面的头和尾,middle呢就是整个layout里面每个item的中间,一般全部加上就对了。

提醒:textview的android:drawable***可以做很多事,千万别忽略它,好歹它上下左右的可以出现个图标,多思考下怎么用最少的控件,写出要求的UI,性能有时候是很重要的。

相关文章

网友评论

    本文标题:你不知道的TextView和LinearLayout

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