Android开发之TextView的初学记录-01

来源:互联网 发布:退出淘宝客 还能加入吗 编辑:程序博客网 时间:2024/05/22 04:45

一、布局

1.代码示例

main_layout.xml :

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"  //设置整体宽度    android:layout_height="match_parent" //设置整体高度    android:orientation="vertical"  //布局属性,防止某些组件无法显示>     <TextView        android:id="@+id/tv_show1"//tv_show1为id名称        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello"        android:textSize="30sp"//设置字体大小/>    <TextView        android:id="@+id/tv_show2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Android"        android:textSize="30sp"/></LinearLayout>
2.具体参数详情
关于高度和宽度设置中的“match_parent”和“wrap_content”:
<以下内容转载自:http://www.cnblogs.com/nikyxxx/archive/2012/06/15/2551390.html>

android布局--Android fill_parent、wrap_content和match_parent的区别

三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精确地指定视图范围更加方便。

1)fill_parent

设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间。

这跟Windows控件的dockstyle属性大体一致。设置一个顶部布局或控件为fill_parent将强制性让它布满整个屏幕。

2) wrap_content

设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。

以TextView和ImageView控件为例,设置为wrap_content将完整显示其内部的文本和图像。

布局元素将根据内容更改大小。设置一个视图的尺寸为wrap_content大体等同于设置Windows控件的Autosize属性为True。

3)match_parent
   Android2.2中match_parent和fill_parent是一个意思 .两个参数意思一样,match_parent更贴切,

于是从2.2开始两个词都可以用。那么如果考虑低版本的使用情况你就需要用fill_parent了。

<以上内容转载自:http://www.cnblogs.com/nikyxxx/archive/2012/06/15/2551390.html>

LinearLayout属性android:orientation

<以下内容转载自:http://blog.sina.com.cn/s/blog_646c39700100onuv.html>

Android布局LinearLayout注意设置属性android:orientation属性,否则有的组件可能无法显示。

该属性不设置时默认为horizontal。此时第一个控件的宽度若设置成“fill_parent”,后面添加的组件将都无法看到。

因此使用该布局的时候要注意设置android:orientation="vertical"。

<以上内容转载自:http://blog.sina.com.cn/s/blog_646c39700100onuv.html>

二、活动代码
package com.example.naruto.myapplication;import android.os.Bundle;        import android.app.Activity;        import android.text.Html;        import android.text.method.LinkMovementMethod;        import android.widget.TextView;public class MainActivity extends Activity {    private TextView textView1,textView2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main_layout);//对应布局文件:main_layout        textView1 = (TextView)findViewById(R.id.tv_show1);//对应id为tv_view的Textview        textView2 = (TextView)findViewById(R.id.tv_show2);        textView1.setText("welcome to ");//可以更改布局中的显示内容        textView1.setTextColor(0xFF950023);    }}

关于setTextColor(int ..)

0xff950023分为4个部分:

ff代表透明度,00表示全透明,ff表示不透明;

95对应RGB中的R

00对应RGB中的G

23对应RGB中的B