Android文本显示:TextView

这篇博客探讨了Android中的TextView组件,用于显示文本和链接。内容涵盖如何在XML和代码中设置TextView的样式,以及如何实现在TextView中添加链接和自定义样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TextView用来显示文本,还可以显示链接

学到的知识点:

1.可以再xml中设置TextView的样式

2.在代码里设置显示的效果

下面的例子主要实现三种在TexTView中添加链接,和在代码中添加TextView样式;

3.实例:

MainActivity.java

package com.textviewlink;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView tv2 = null;
	private TextView tv3 = null;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv2 = (TextView)findViewById(R.id.tv2);
        //设置tv2可点击必须加,不然不能点击
        tv2.setMovementMethod(LinkMovementMethod.getInstance());
        
        tv3 = (TextView)findViewById(R.id.tv3);
        //tv3 html代码实现连接
        tv3.setText(Html.fromHtml("方法三" + "<a href=\"http://www.baidu.com\">百度</a>"));
        tv3.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
}
main.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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/link1"
    android:textSize="30px"
    android:autoLink="all"/>
<!-- android:autoLink="all" 属性可以为所有类型添加链接 -->
<TextView 
    android:id="@+id/tv2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="30px"
    android:text="@string/link2"/>
<TextView 
    android:id="@+id/tv3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="30px"/>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">TextViewLink</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
	<string name="link1">方法一点击http://www.baidu.com</string>
	<string name="link2">方法二<a href="http://www.baidu.com">百度</a> 
	    <a href="http://www.google.com">google</a></string>
	<!-- 在方法二当中,需要在MainActivity中使用setMovementMethod()来实现激活链接,否则不能点击 -->
</resources>

4效果图:

5.代码设置样式

package com.textview;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView tv1 = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);
        tv1 = (TextView)findViewById(R.id.tv1);
        tv1.setText("利用代码来设置Textview的显示样式");
        tv1.setTextColor(Color.BLUE);
        tv1.setTextSize(30);
        tv1.setBackgroundColor(Color.RED);
        tv1.setPadding(10, 10, 10, 10);
        tv1.setVisibility(0);
        /*
        android view的setVisibility方法值有3个:
		1.View.VISIBLE,常量值为0,意思是可见的
		2.View.INVISIBLE,常量值是4,意思是不可见的
		3.View.GONE,常量值是8,意思是不可见的,并且不占用布局空间,就是根据值得不同显示效果也不同
         * */
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值