关于TextView的一些学习

来源:互联网 发布:缺少对象 js 编辑:程序博客网 时间:2024/05/29 02:08
        <TextView
            android:id="@+id/homepage_btn"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/btn_home_footbar_bg" //背景色
            android:drawableTop="@drawable/home_icon_recommend_normal"  //在文字上面显示的图片资源id
            android:gravity="center" //居中
            android:text="@string/recommend"

            android:textColor="@color/gray2_color" />


home_icon_recommend_normal

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_window_focused="false" android:drawable="@drawable/home_bg_nav_normal"></item>    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/home_bg_nav_pressed"></item>    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/home_bg_nav_pressed"></item>    <item android:state_selected="true" android:drawable="@drawable/home_bg_nav_pressed"></item>    <item android:state_focused="true" android:drawable="@drawable/home_bg_nav_pressed"></item></selector>



TextView 的setCompoundDrawablesWithIntrinsicBounds与setCompoundDrawables的区别


我做了一个测试,大概推出这样的结论:
setCompoundDrawables 画的drawable的宽高是按drawable.setBound()设置的宽高,所以才有The Drawables must already have had setBounds(Rect) called.这句话之说。
而setCompoundDrawablesWithIntrinsicBounds是画的drawable的宽高是按drawable固定的宽高,即通过getIntrinsicWidth()与getIntrinsicHeight()获得,所以才有The Drawables' bounds will be set to their intrinsic bounds.这句话之说!


private TextView mTV;

homepageNormal = getResources().getDrawable(R.drawable.home_icon_recommend_normal);

textColorNormal = getResources().getColor(R.color.gray);

mTV.setSelected(false);
mTV.setTextColor(textColorNormal);
mTV.setCompoundDrawablesWithIntrinsicBounds(null, homepageNormal, null, null);



0 0
原创粉丝点击