http://snowolf.iteye.com/blog/465433
一:新建HelloTextView 工程
新建一个Hello world详细步骤可以参见
Android教程之三:第一个Android应用,HelloWorld。创建设置如下:
然后运行该应用就可以看到TextView的效果,是显示一行字:“Hello World, HelloTextView!”,这是因为新建的Hello项目自带的一个TextView。
二:分析TextView组件TextView是Android中常用的组件之一,可以用他来显示文字,就像一个标签一样,或者你可以认为是html中的span。对于TextView我们最关心的应该是怎么设置显示的文本,怎样设置字体的大小,字体的颜色,字体的样式,
其实很简单,TextView中提供了大量的属性帮我们配置TextView。
1.修改xml配置文件实现。
我们修改main.xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#ff0000" android:textSize="24sp" android:text android:text="@string/hello" /> </LinearLayout>
这里增加了三个属性的设置,分别是android:textColor="#ff0000"设置字体为红色,android:textSize="24sp"设置字体为24sp, android:text设置字体加粗,预览效果如下图:
看到我们的TextView的内容已经变成红色,24sp大,加粗。。
2.修改java代码实现。
同样我们不修改xml文件,而是通过java编码来实现上面的图示效果,首先我们先给这个TextView分配一个id,也就是这个TextView的标记记号,方便我们找到他。在main.xml的TextView中加入android:id="@+id/text_view"就可以为该TextView分配一个id。。这里@+id/是表示在R类的id类下新增常量字段,这里的常量字段是text_view。
下面修改HelloTextView类如下:
package com.flysnow; import android.app.Activity; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.util.TypedValue; import android.widget.TextView; public class HelloTextView extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//设置内容显示的xml布局文件 TextView textView=(TextView)findViewById(R.id.text_view);//取得我们的TextView组件 textView.setTextColor(Color.RED);//设置成红色 textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24f);//设置成24sp textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗 } }
最终结果和上图一样的,这说明通过代码和xml配置都可以定制TextView,但是推荐使用xml进行定制,使用java代码控制逻辑,这符合mvc模式,也符合Android的设计思想。
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">我的博客地址是:http://flysnow.iteye.com \n我的电话是:400-34534-500\n我的email是12235@163.com</string> <string name="app_name">HelloTextView</string> </resources>修改main.xml为:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/text_view" android:autoLink="web" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>然后把HelloTextView类中的那一段设置文本颜色、大小和样式的代码注释掉,运行程序就会看到如下图的效果:
当我们点击蓝色的我的博客的网址的时候,Android系统就会调用默认的web浏览器打开我的博客。
有的朋友已经注意到了,文本里我还写了我的电话和email,难道TextView也支持电话和email超链接吗?没错,的确支持,当我们设置android:autoLink="phone"的时候,文本里的电话就会变成蓝色超链接形式,点击就会打开拨号界面等待你按通话键拨号,email也是同理。。
当我们把 android:autoLink换成phone的时候发现网址不超连接了,换成email也是一样。难道我们不能一下子让网址,电话,email都超链接吗?答案是肯定的,这时候我们可以把 android:autoLink设置成all,这样里面的网址、电话和email就都可以超链接了。
2.跑马丁效果。有时候我们要显示的文本较长,TextView不能完全显示,这时候可以通过这中跑马灯的方式让文本移动展示,达到了既不占用地方又能完全看到文本的目的。这里直接复用农民伯伯的跑马灯代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/text_view" android:autoLink="all" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:ellipsize="marquee" android:focusable="true" android:marqueeRepeatLimit="marquee_forever" android:focusableInTouchMode="true" android:scrollHorizontally="true"/> </LinearLayout>
然后为TextView设置一个很长的字符串。运行就可以看到横向移动的效果。
设置的属性说明可以参考TextView的文档,这里也有中文版本:TextView中文API文档
这里要进行说明的是:以上设置在大部分情况下都会成功的展示跑马灯样式,但是在一些复杂的布局中就会看不到任何文字。比如我开发的Android应用“我团”,在展示团购详细信息页面,我自定义了一个标题栏让其显示团购的信息,想让其跑马灯的方式显示,但是使用了上述代码后看不到文字,其实是文字被撑下来的,这时候我们设置android:singleLine="true"以单行的方式展示就好了。所以请以后实现跑马灯效果的时候最好加上android:singleLine="true"单行展示。。
四:小结好了,一个TextView也啰嗦了那么多,希望大家能听明白,最后两个实用的例子能帮助大家更好的学习和使用TextView。。这两天JE挂了,没来得及更新,对不起大家了。最近忙于升级“我团”应用,也只能网上10点后才能上网写。可能更新会很慢,请见谅,谢谢各位的支持
转自:http://flysnow.iteye.com/blog/822358
写在AndroidManifest.xml 里
像这个样子:
</application> <uses-permission android:name="android.permission.RUN_INSTRUMENTATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> </uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"> </uses-permission>
String
ACCESS_CHECKIN_PROPERTIES
Allows read/write access to the "prop
Technorati Tags: Android,permission,Manifest
erties" table in the checkin database, to change values that get uploaded.
String
ACCESS_COARSE_LOCATION
Allows an application to access coarse (e.g., Cell-ID, WiFi) location
String
ACCESS_FINE_LOCATION
Allows an application to access fine (e.g., GPS) location
String
ACCESS_LOCATION_EXTRA_COMMANDS
Allows an application to access extra location provider commands
String
ACCESS_MOCK_LOCATION
Allows an application to create mock location providers for testing
String
ACCESS_NETWORK_STATE
Allows applications to access information about networks
String
ACCESS_SURFACE_FLINGER
Allows an application to use SurfaceFlinger's low level features
String
ACCESS_WIFI_STATE
Allows applications to access information about Wi-Fi networks
String
ACCOUNT_MANAGER
Allows applications to call into AccountAuthenticators.
String
AUTHENTICATE_ACCOUNTS
Allows an application to act as an AccountAuthenticator for the AccountManager
String
BATTERY_STATS
Allows an application to collect battery statistics
String
BIND_APPWIDGET
Allows an application to tell the AppWidget service which application can access AppWidget's data.
String
BIND_INPUT_METHOD
Must be required by input method services, to ensure that only the system can bind to them.
String
BLUETOOTH
Allows applications to connect to paired bluetooth devices
String
BLUETOOTH_ADMIN
Allows applications to discover and pair bluetooth devices
String
BRICK
Required to be able to disable the device (very dangerous!).
String
BROADCAST_PACKAGE_REMOVED
Allows an application to broadcast a notification that an application package has been removed.
String
BROADCAST_SMS
Allows an application to broadcast an SMS receipt notification
String
BROADCAST_STICKY
Allows an application to broadcast sticky intents.
String
BROADCAST_WAP_PUSH
Allows an application to broadcast a WAP PUSH receipt notification
String
CALL_PHONE
Allows an application to initiate a phone call without going through the Dialer user interface for the user to confirm the call being placed.
String
CALL_PRIVILEGED
Allows an application to call any phone number, including emergency numbers, without going through the Dialer user interface for the user to confirm the call being placed.
String
CAMERA
Required to be able to access the camera device.
String
CHANGE_COMPONENT_ENABLED_STATE
Allows an application to change whether an application component (other than its own) is enabled or not.
String
CHANGE_CONFIGURATION
Allows an application to modify the current configuration, such as locale.
String
CHANGE_NETWORK_STATE
Allows applications to change network connectivity state
String
CHANGE_WIFI_MULTICAST_STATE
Allows applications to enter Wi-Fi Multicast mode
String
CHANGE_WIFI_STATE
Allows applications to change Wi-Fi connectivity state
String
CLEAR_APP_CACHE
Allows an application to clear the caches of all installed applications on the device.
String
CLEAR_APP_USER_DATA
Allows an application to clear user data
String
CONTROL_LOCATION_UPDATES
Allows enabling/disabling location update notifications from the radio.
String
DELETE_CACHE_FILES
Allows an application to delete cache files.
String
DELETE_PACKAGES
Allows an application to delete packages.
String
DEVICE_POWER
Allows low-level access to power management
String
DIAGNOSTIC
Allows applications to RW to diagnostic resources.
String
DISABLE_KEYGUARD
Allows applications to disable the keyguard
String
DUMP
Allows an application to retrieve state dump information from system services.
String
EXPAND_STATUS_BAR
Allows an application to expand or collapse the status bar.
String
FACTORY_TEST
Run as a manufacturer test application, running as the root user.
String
FLASHLIGHT
Allows access to the flashlight
String
FORCE_BACK
Allows an application to force a BACK operation on whatever is the top activity.
String
GET_ACCOUNTS
Allows access to the list of accounts in the Accounts Service
String
GET_PACKAGE_SIZE
Allows an application to find out the space used by any package.
String
GET_TASKS
Allows an application to get information about the currently or recently running tasks: a thumbnail representation of the tasks, what activities are running in it, etc.
String
GLOBAL_SEARCH
This permission can be used on content providers to allow the global search system to access their data.
String
HARDWARE_TEST
Allows access to hardware peripherals.
String
INJECT_EVENTS
Allows an application to inject user events (keys, touch, trackball) into the event stream and deliver them to ANY window.
String
INSTALL_LOCATION_PROVIDER
Allows an application to install a location provider into the Location Manager
String
INSTALL_PACKAGES
Allows an application to install packages.
String
INTERNAL_SYSTEM_WINDOW
Allows an application to open windows that are for use by parts of the system user interface.
String
INTERNET
Allows applications to open network sockets.
String
MANAGE_ACCOUNTS
Allows an application to manage the list of accounts in the AccountManager
String
MANAGE_APP_TOKENS
Allows an application to manage (create, destroy, Z-order) application tokens in the window manager.
String
MASTER_CLEAR
String
MODIFY_AUDIO_SETTINGS
Allows an application to modify global audio settings
String
MODIFY_PHONE_STATE
Allows modification of the telephony state - power on, mmi, etc.
String
MOUNT_FORMAT_FILESYSTEMS
Allows formatting file systems for removable storage.
String
MOUNT_UNMOUNT_FILESYSTEMS
Allows mounting and unmounting file systems for removable storage.
String
PERSISTENT_ACTIVITY
Allow an application to make its activities persistent.
String
PROCESS_OUTGOING_CALLS
Allows an application to monitor, modify, or abort outgoing calls.
String
READ_CALENDAR
Allows an application to read the user's calendar data.
String
READ_CONTACTS
Allows an application to read the user's contacts data.
String
READ_FRAME_BUFFER
Allows an application to take screen shots and more generally get access to the frame buffer data
String
READ_HISTORY_BOOKMARKS
Allows an application to read (but not write) the user's browsing history and bookmarks.
String
READ_INPUT_STATE
Allows an application to retrieve the current state of keys and switches.
String
READ_LOGS
Allows an application to read the low-level system log files.
String
READ_OWNER_DATA
Allows an application to read the owner's data.
String
READ_PHONE_STATE
Allows read only access to phone state.
String
READ_SMS
Allows an application to read SMS messages.
String
READ_SYNC_SETTINGS
Allows applications to read the sync settings
String
READ_SYNC_STATS
Allows applications to read the sync stats
String
REBOOT
Required to be able to reboot the device.
String
RECEIVE_BOOT_COMPLETED
Allows an application to receive the ACTION_BOOT_COMPLETED that is broadcast after the system finishes booting.
String
RECEIVE_MMS
Allows an application to monitor incoming MMS messages, to record or perform processing on them.
String
RECEIVE_SMS
Allows an application to monitor incoming SMS messages, to record or perform processing on them.
String
RECEIVE_WAP_PUSH
Allows an application to monitor incoming WAP push messages.
String
RECORD_AUDIO
Allows an application to record audio
String
REORDER_TASKS
Allows an application to change the Z-order of tasks
String
RESTART_PACKAGES
Allows an application to restart other applications.
String
SEND_SMS
Allows an application to send SMS messages.
String
SET_ACTIVITY_WATCHER
Allows an application to watch and control how activities are started globally in the system.
String
SET_ALWAYS_FINISH
Allows an application to control whether activities are immediately finished when put in the background.
String
SET_ANIMATION_SCALE
Modify the global animation scaling factor.
String
SET_DEBUG_APP
Configure an application for debugging.
String
SET_ORIENTATION
Allows low-level access to setting the orientation (actually rotation) of the screen.
String
SET_PREFERRED_APPLICATIONS
This constant is deprecated. No longer useful, see addPackageToPreferred(String) for details.
String
SET_PROCESS_LIMIT
Allows an application to set the maximum number of (not needed) application processes that can be running.
String
SET_TIME_ZONE
Allows applications to set the system time zone
String
SET_WALLPAPER
Allows applications to set the wallpaper
String
SET_WALLPAPER_HINTS
Allows applications to set the wallpaper hints
String
SIGNAL_PERSISTENT_PROCESSES
Allow an application to request that a signal be sent to all persistent processes
String
STATUS_BAR
Allows an application to open, close, or disable the status bar and its icons.
String
SUBSCRIBED_FEEDS_READ
Allows an application to allow access the subscribed feeds ContentProvider.
String
SUBSCRIBED_FEEDS_WRITE
String
SYSTEM_ALERT_WINDOW
Allows an application to open windows using the type TYPE_SYSTEM_ALERT, shown on top of all other applications.
String
UPDATE_DEVICE_STATS
Allows an application to update device statistics.
String
USE_CREDENTIALS
Allows an application to request authtokens from the AccountManager
String
VIBRATE
Allows access to the vibrator
String
WAKE_LOCK
Allows using PowerManager WakeLocks to keep processor from sleeping or screen from dimming
String
WRITE_APN_SETTINGS
Allows applications to write the apn settings
String
WRITE_CALENDAR
Allows an application to write (but not read) the user's calendar data.
String
WRITE_CONTACTS
Allows an application to write (but not read) the user's contacts data.
String
WRITE_EXTERNAL_STORAGE
Allows an application to write to external storage
String
WRITE_GSERVICES
Allows an application to modify the Google service map.
String
WRITE_HISTORY_BOOKMARKS
Allows an application to write (but not read) the user's browsing history and bookmarks.
String
WRITE_OWNER_DATA
Allows an application to write (but not read) the owner's data.
String
WRITE_SECURE_SETTINGS
Allows an application to read or write the secure system settings.
String
WRITE_SETTINGS
Allows an application to read or write the system settings.
String
WRITE_SMS
Allows an application to write SMS messages.
String
WRITE_SYNC_SETTINGS
Allows applications to write the sync settings