TextView就是用来显示文本标签的控件,修改使用TextView显示文本的颜色、大小等属性。
实例代码:
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/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textSize="18sp" android:text="@string/hello"/> </LinearLayout>
Activity:
public class MainActivity extends AppCompatActivity { /** * 声明TextView控件对象 */ private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = findViewById(R.id.textview); // 设置TextView显示文本 String helloTextView = "TextView实例,欢迎使用!"; // 设置文本的颜色 mTextView.setTextColor(getResources().getColor(R.color.red)); // 设置文本字体大小 mTextView.setTextSize(20); // 设置文本字体背景 mTextView.setBackgroundColor(getResources().getColor(R.color.blue)); // 设置文本字体背景 mTextView.setText(helloTextView); } }
因为在代码清单中使用了fundViewById来获得TextView对象,因此在布局文件“activity_main.xml”中需要指定TextView资源的ID
TextView对象方法与对应的XML属性
TextView对象方法 | XML属性 |
setTextColor | android:textColor |
setTextSize | android:textSize |
setText | android:text |
setBackgroundResource | android:background |
setHeight/setWidth | android:height/android:width |
效果图:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119112.html