一步一步学android控件(之二) —— TextView

android 控件众多 , 额 , 具体多少个呢? 貌似有那么几十个吧,也没做个统计,嘿嘿!......

有木有朋友感觉写了那么长时间的android代码,有时候想写点自己的东西的时候却发现自己好像离不开网络耶,什么都需要先到网络上遨游一番才能解决自己的问题。思前想后,个人觉得还是有必要巩固一下自己学习过的东西——想想以前这些东西,自己都写过一遍了,但是折腾一段时间下来都不知道放哪里去了........


好了,废话不多说了,这次准备重新学习一下android的常用控件TextView、EditText、AutoCompleteTextView、Button、CalendarView、CheckBox、Chronometer、CompoundButton、DatePicker、DigitalClock、ExpandableListView、Gallery、GridView、HorizontalScrollView、ImageButton、ImageSwitcher、ImageView、ListPopupWindow、ListView、MultiAutoCompleteTextView、NumberPicker、PopupMenu、PopupWindow、ProgressBar、QuickContactBadge、RadioButton、RadioGroup、RatingBar、RemoteViews、ScrollView、SearchView、SeekBar、SlidingDarwer、Switch、TableHost、TextClock、TextSwitcher、TimePicker、Toast、ToggleButton、VideoView、ViewFlipper、ViewSwitcher、ZoomButton等控件。


今天学习TextView控件,先来看看效果图(注意:本文中的代码是写在工程SelfDefineWidget中的,具体内容参见一步一步学android控件(之一) —— 开始篇


该界面中有5个button和一个TextView控件,当点击每个button时将修改TextView中的内容或背景:

1、点击button “指定字体大小为24” 将看到字体变大,对应属性 android:textSize.

2、点击button “指定字体颜色为蓝色” 字体颜色将从黑色变为男色,对应属性android:textColor.

3、点击button “html下划线” ,将在字体的下面画出一条直线。可以在strings.xml文件中用<u>Some thind </u> 替换硬编码。

4、点击button “html删除线”,将在TextView中的字体的中间画一条直线。可以在strings.xml文件中用<stroke>Some thind </stroke> 替换硬编码。

5、点击button “自定义背景” ,将看到以颜色#e0FFFFCC为底色,#e066CC00为边框色,边框宽度为2的椭圆。

其中1,2,3,4的效果比较简单这里就不给出效果图了,下图为点击“自定义背景”后看到的textView的背景:


从上图可以看到,我们点击了删除线、将字体设置为蓝色,自定义背景的button 。

下面一步一步实现上述所述的功能:

1、首先,打开一步一步学android控件(之一) —— 开始篇 创建的工程,在res/strings.xml文件中加入如下内容:

<!-- strings for TextView -->
    <string name="default_text_view_str">这是默认的TextView的样式</string>
    
    <string name="customer_font_size">指定字体大小为24</string>
    <string name="font_size_24">这是24号大小的字体哦</string>
    
    <string name="customer_font_color">指定字体颜色为蓝色</string>
    
    <string name="html_u">html下划线</string>
    <string name="html_stroke">html删除线</string>
    
    <string name="cumtomer_bg_str">自定义背景</string>
    <!-- end -->
2、在res/values目录下面创建文件widget_color.xml (该文件用来定义所有的颜色),内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="text_view_fill">#e0FFFFCC</color>
    <color name="text_view_stroke">#e066CC00</color>
    <color name="color_blue">#0000ff</color>
</resources>
3、在drawable(如果不存在,自己新建一个)目录下创建widget_text_view_bg.xml文件(自定义的背景),内容如下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <solid android:color="@color/text_view_fill" />

    <stroke
        android:width="2dp"
        android:color="@color/text_view_stroke" />

    <padding
        android:bottom="10dp"
        android:left="15dp"
        android:right="15dp"
        android:top="10dp" />

</shape>

可以看到,定义了一个shape(对应ShapeDrawable类),stroke 描边框 , solid 指定填充颜色,padding 指定内容和边框的距离。


4、创建效果图中的界面text_view_detail.xml 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <Button
            android:id="@+id/customer_font_size_btn"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/customer_font_size" />

        <Button
            android:id="@+id/customer_font_color_btn"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/customer_font_color" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout1" >
        <Button
            android:id="@+id/html_u_btn"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/html_u" />

        <Button
            android:id="@+id/html_stroke_btn"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/html_stroke" />
        <Button
            android:id="@+id/cumtomer_bg_btn"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cumtomer_bg_str" />
    </LinearLayout>

    <TextView
        android:id="@+id/show_text_view_detail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="100dp"
        android:layout_marginLeft="16dp"
        android:singleLine="true"
        android:text="@string/default_text_view_str" />

</RelativeLayout>

5、创建用于交互的activity —— WidgetTextView.java

package com.xy.zt.selfdefinewieget;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.Html;
import android.text.TextPaint;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class WidgetTextView extends Activity implements OnClickListener{

    private static final float FONT_SIZE = 24f;
    private Button mCusFontSize ;
    private Button mCusFontColor ;
    private Button mHtmlU ;
    private Button mHtmlStroke ;
    private Button mCusBg ;
    
    private TextView mShowView ;
    private Resources mRes;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text_view_detail);
        init();
    }

    void init(){
        mRes = getResources();
        mShowView = (TextView) findViewById(R.id.show_text_view_detail);
        
        mCusFontSize = (Button) findViewById(R.id.customer_font_size_btn);
        mCusFontSize.setOnClickListener(this);
        
        mCusFontColor = (Button) findViewById(R.id.customer_font_color_btn);
        mCusFontColor.setOnClickListener(this);
        
        mHtmlU = (Button) findViewById(R.id.html_u_btn);
        mHtmlU.setOnClickListener(this);
        
        mHtmlStroke = (Button) findViewById(R.id.html_stroke_btn);
        mHtmlStroke.setOnClickListener(this);
        
        mCusBg= (Button) findViewById(R.id.cumtomer_bg_btn);
        mCusBg.setOnClickListener(this);
    }

    public void onClick(View v) {
        String tempStr ;
        switch(v.getId()){
        case R.id.customer_font_size_btn:
            mShowView.setText(R.string.font_size_24);
            mShowView.setTextSize(FONT_SIZE);
            break;
        case R.id.customer_font_color_btn:
            mShowView.setTextColor(mRes.getColor(R.color.color_blue));
            break;
        case R.id.html_u_btn:
            tempStr = mShowView.getText().toString();
            mShowView.getPaint().setAntiAlias(true);
            mShowView.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
            break;
        case R.id.html_stroke_btn:
            mShowView.getPaint().setAntiAlias(true);
            mShowView.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            break;
        case R.id.cumtomer_bg_btn:
            mShowView.setBackgroundResource(R.drawable.widget_text_view_bg);
            break;
        }
    }
}

6、做好这一切后,还需要在 一步一步学android控件(之一) —— 开始篇 中WidgetsAdapter类中修改handleItemClicked函数为如下内容:

Intent intent = new Intent();
        switch (action) {
        case ViewData.TEXT_VIEW_ID:
            intent.setClass(mContext, WidgetTextView.class);
            mContext.startActivity(intent);
            break;
        }

ok,今天就到这里,下一个控件——Button 。

2015-07-28增加-------------------------------------------------------

android 设置字体加粗方法

1.在xml文件中直接配置

android:textStyle="bold"
发现没有效果

2.直接在代码里使用

TextView tv = new TextView(context);
tv.getPaint().setFakeBoldText(true);
明显感觉到加粗的效果
-----------------------------done-------------------------------------------


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值