Android版的TextView [英] Android TextView

查看:254
本文介绍了Android版的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的一个例子,我尝试运行。
有2种方式来做到这一点。

I have an example in android that I am trying to run. There were 2 ways to do it..

// Get a drawable 
ColorDrawble redDrawable = (ColorDrawable).getResources().getDrawable(R.drawable.red_rectangle); 

//Set it as a background to a text view 
textView.setBackground(redDrawable); 

当我把这个在Eclipse IDE中我得到一个错误ColorDrawble不能被解析为一个类型
我在主XML TextView的

When I put this in the Eclipse IDE I get an error ColorDrawble cannot be resolved to a type I have the textview in the 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:id="@+id/texter"
          android:layout_height="wrap_content" 
          android:text="this string"/>


</LinearLayout>


和在琴弦xml文件的资源


and the resources in the strings xml file

<resources>
    <string name="hello">Hello World, ResourceTesterActivity!</string>
    <string name="app_name">ResourceTester</string>
     <drawable name="red_rectangle" >#f00</drawable> 
    <drawable name="blue_rectangle">#0000ff</drawable> 
    <drawable name="green_rectangle" >#f0f0</drawable>
</resources>

**

推荐答案

您可以简单地使用可绘制这样的:

you can simply use a drawable like this :

// Get a drawable 
Drawable redDrawable = YourActivity.this.getResources().getDrawable(R.drawable.red_rectangle); 

//Set it as a background to a text view 
textView.setBackgroundDrawable(redDrawable);//i've changed setBackground with setBackgroundDrawable.

或者你可以直接使用:

or you can directly use :

textView.setBackgroundResources(R.drawable.red_rectangle);

注意:清洁,重新生成项目,并运行它来测试
并为可绘制,你不需要声明它的strings.xml,只需添加上的文件夹可绘制的绘制,并且它会工作

NOTE : clean and rebuild your project , and run it to test and for the drawable, you dont need to declare it in strings.xml , just add your drawable on the folder drawables , and it will work

这篇关于Android版的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆