2013年3月11日

来源:互联网 发布:eregi() 网页源码 编辑:程序博客网 时间:2024/05/16 09:14

1、TextView里的setText方法支持一下多态构造方法:

      

 public final void setText (CharSequence text)       public final void setText (int resid)       public void setText (CharSequence text, TextView.BufferType type)       public final void setText (int resid, TextView.BufferType type)       public final void setText (char[] text, int start, int len)

 

       在此,以最后public final void setText (char[] text, int start, int len)为例,第一个参数为char数组作为输出数据,第二个参数为从哪一个元素索引开始选取,第三个参数则为要取出多少个元素,例如:

      Char[] char = {D , a , v , i , d};

      textView01.setText(char , 1 , 3);

      上述程序输出的结果是”avi”,懂了吧?但是要注意TextView.setText()不支持HTML TAG的输出,所以即便写成这样:

      textView.setText(“<a href=\”http://blog.csdn.net/zhai56565\”>小武的博客</a>”);

      实际输出时也就是纯文本而已,不会作HTML TAG转换。但是在TextView里加上Android:autoLink=”all”,那么正文中有网址(http://),则可以显示出来,例如:

       

<TextView              android:id="@+id/textView01"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:autoLink="all"              android:text="请访问我的博客:http://blog.csdn.net/zhai56565" />


 

2、drawable定义颜色常数的方法

       事先将定义好的颜色代码(color code)以drawable的名称(name)存放于resource当中,这是学习开发Android程序必须养成的好的习惯,正如存储字符串常数一样,使用如下两种方式:

<drawable name = “black”>#000000 </drawable>

<color name="white">#ffffff</color>      

定义好的drawable name常数必须存放在res/values下面作为资源使用,但定义好的颜色并非只能在XML中使用,也可以通过程序来更改:

             

Resources resources = getResources();              Drawable drawable = resources.getDrawable(R.drawable.black);              textView01.setBackgroundDrawable(drawable);              int color = resources.getColor(R.color.white);              textView01.setBackgroundColor(color);


下面是色值表大全http://blog.csdn.net/zhai56565/article/details/8087924

 

3、取得手机屏幕的大小:

              Display display = getWindowManager().getDefaultDisplay();

              int Width = display.getWidth();

              int height = display.getHeight();

      

4、使用Style样式简化程序代码

       下面在res/values/styles.xml中定义textView的样式:

      

<resources xmlns:android="http://schemas.android.com/apk/res/android">       <style name="TextView_Top">              <item name="android:textSize">18dp</item>              <item name="android:textColor">@color/white</item>       </style>       <style name="TextView_Bottom">              <item name="android:textSize">130dp</item>              <item name="android:textColor">@drawable/black</item>              <item name="android:textStyle">bold</item>              <item name="android:shadowColor">@color/white</item>              <item name="android:shadowDx">4dp</item>              <item name="android:shadowDy">4dp</item>              <item name="android:shadowRadius">2dp</item>       </style></resources>


下面将样式绑定在TextView上:

      

<TextView              android:id="@+id/textView01"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              style="@style/TextView_Top"              android:autoLink="all"              android:text="请访问我的博客:http://blog.csdn.net/zhai56565" />

 

 

 

原创粉丝点击