textview的属性?android:attr/

?意味着你引用类型属性,?android:attr/listPreferredItemHeight意味着你用android命名空间内的listPreferredItemHeight这种属性。总的来说就是说给layout_height指定系统预定义的一个属性值。

?android:attr/textAppearanceLargeInverse //系统自带的一个外观

?表示系统是否有这种外观,否则使用默认的外观。 可选值:textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSmallInverse

  •  textView.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG ); //下划线
  • textView.getPaint().setAntiAlias(true);//抗锯齿
  • textview.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG); //中划线
  • setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);  // 设置中划线并加清晰
  • textView.getPaint().setFlags(0);  // 取消设置的的划线

android:drawablePadding

android:drawablePadding设置text与drawable(图片)的间隔,与drawableLeft、 drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。 android:drawablePadding 给上下左右的图片加间隔

setCompoundDrawablesWithIntrinsicBounds

TextView也可以直接绑定一个drawable对象显示的

  • void  setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) 或
  • void  setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) // int类型指定的资源id,方位则是textview什么位置显示drawable对象, 如left = Resource identifier of the left Drawable. 相当于:android:drawableLeft: The drawable to be drawn to the left of the text.
  • void  setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)

setCompoundDrawables(Drawable left, top, right, bottom)

  • 画的drawable的宽高是按drawable.setBound()设置的宽高,所以使用之前必须使用Drawable.setBounds设置Drawable的长宽, The Drawables must already have had setBounds(Rect) called
    •  Drawable d1 = rr.getCompoundDrawables()[1];  //此处取的是android:drawableTop的图片
      drawable.setBounds(0, 0, d1.getIntrinsicWidth(), d1.getIntrinsicHeight());//  getIntrinsicWidth()取得的是Drawable在手机上的宽度,所以不同分辨率下获取到的值是不同的,关键所在处
  • setCompoundDrawablesWithIntrinsicBoundsdrawable画的宽高是按drawable固定的宽高,即通过getIntrinsicWidth()与getIntrinsicHeight()获得,The Drawables’ bounds will be set to their intrinsic bounds
  • 要想达到相同效果: 自定义zoomDrawable()

Drawable draw1 = this.getResources().getDrawable(R.drawable.srvmng);

int w = draw1.getIntrinsicWidth();    

        int h = draw1.getIntrinsicHeight();    

Rect rect = draw1.getBounds();

text1.setCompoundDrawablesWithIntrinsicBounds(null, zoomDrawable(draw1,32,32), null,null);

<TextView
android:id=”@+id/item1″
android:layout_width=”fill_parent”
android:layout_height=”40.0dip”
android:layout_alignLeft=”@+id/scenic_icon”
android:layout_below=”@+id/consume”
android:layout_marginTop=”10dp”
android:background=”@drawable/scenic_details_item_bg”
android:drawableLeft=”@drawable/sight_1
android:drawablePadding=”10.0dip”
android:drawableRight=”@drawable/back_light
android:gravity=”center_vertical”
android:padding=”10.0dip”
android:text=”景区地图自动导游”
android:textColor=”@color/grey_textcolor” />

让TextView后面的Layout响应Click事件

有一个Layout(LinearLayout或者RelativeLayout)作为背景,他有一个TextView作为子元素,当点击TextView控件时,想让它所在的Layout响应。这个问题其实有两个解决方法:

  • 在它的父Layout中声明clickable为true: 这种方法判断长按事件有时候会失效

<TextView
android:id=”@+id/my_recent_voice_edit”
android:layout_width=”148.3dp”
android:layout_height=”wrap_content”
android:duplicateParentState=”true”
android:gravity=”center”
android:text=”@string/voice_recent_btn_text”
android:textColor=”@color/textviewcolor
android:layout_marginBottom=”1dp”
android:clickable=”true” //可点击
android:textSize=”12sp” />

  • 屏蔽Layout的子元素获取焦点。后一种方法中不能为TextView指定selector了

<LinearLayout
android:id=”@+id/voice_recent_edit_layout”
android:layout_width=”193.3dp”
android:layout_height=”wrap_content”
android:layout_marginLeft=”33dp”
android:layout_marginRight=”33dp”
android:background=”@drawable/recent_voice_bg”
  android:clickable=”true”
            android:descendantFocusability=”blocksDescendants”
android:gravity=”center_vertical”
android:visibility=”gone” >

定义颜色res/color/textviewcolor.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<selector xmlns:android=”http://schemas.android.com/apk/res/android”>
<item android:state_selected=”true” android:color=”#ff0000″ />
<item android:state_focused=”true” android:color=”#ff0000″ />
<item android:state_pressed=”true” android:color=”#ff0000″ />
<item android:color=”#89683B” />
</selector>

E.G.

image imageimage

三个按钮是三个TextView派生类实例,中间的白线,是1px宽的白色矩形:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"

android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/background_color">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="10dip" />
<LinearLayout android:layout_width="fill_parent"
android:layout_height="40dip">

<com.easymorse.textbutton.TextButton
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1" android:text="电影" android:gravity="center_vertical|center_horizontal"
android:background="@drawable/button" android:focusable="true" android:clickable="true"/>

<View android:layout_width="2px" android:layout_height="fill_parent" android:background="#ffffffff" />

<com.easymorse.textbutton.TextButton android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true"
android:layout_weight="1" android:text="电视" android:gravity="center_vertical|center_horizontal"
android:background="@drawable/button" android:focusable="true" />

<View android:layout_width="2px" android:layout_height="fill_parent"android:background="#ffffffff" />

<com.easymorse.textbutton.TextButton android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true"
android:layout_weight="1" android:text="明星" android:gravity="center_vertical|center_horizontal"
android:background="@drawable/button" android:focusable="true" />

</LinearLayout>
</LinearLayout>

在按钮布局中要使用android:focusable=”true” android:clickable=”true”,这样才能比如通过轨迹球聚焦到按钮上,才能用手触摸按钮的时候触发事件, 点击按钮变色,主要在android:background=”@drawable/button”配置,button配置了点击事件发生后的背景色改变,而不需要编写代码

TextView中的个别字设置为超链接,或者设置个别字的颜色、字体等,那就需要用到Spannable对象

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: