Android入门之基本资源的使用(小白速成4)

之前略微提到过,可以将常用的属性定义在res中,颜色、字符、格式等也可以这样。

常用颜色设置

如在values文件夹下新建一个颜色文档,里面这样写:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="text_color">#ff0000</color>
    <color name="bg_color">#00ff00</color>
</resources>

我们就可以将较常使用的颜色赋予名字,更为方便调用

调用也分为两种方式,代码方式和参数方式
下面我们看调用代码:

public void test(View view){
   
   
        int color =this.getResources().getColor(R.color.bg_color);//获得颜色
        Toast.makeText(this,""+color,Toast.LENGTH_SHORT).show();//或者在main中TextView 写android:textColor="@color/text_color"

        this.getWindow().setBackgroundDrawableResource(R.color.bg_color);

    }

代码方式我们可以用getResources() 方法获得我们定义的颜色,并且更改文字或者背景颜色。而参数方式,我们只需像注释中写的那样,增加textColor属性。

常用字符串设置

如同颜色一样,我们也需要在values文件夹下的string文件中自定义我们的字符串:

<resources>
    <string name="app_name">test_color</string>
    <string name="yyj">你好,我是Embers</string>>
</resources>

实际使用的时候,可以通过这样的格式:

android:text="@string/yyj"

我们就可以给文本赋予提前定义好的值
而在代码中:

public void test2(View view){
   
   
        String str =this.getString(R.string.yyj);
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();

        Button button=findViewById(R.id.button2);
        button.setText(R.string.yyj);
    }

我们一般通过R.string.名字来寻找我们的字符串。
而上面代码是两种方式,第一种是先拿到再使用,第二种是直接使用。这两种都比较方便。

尺寸资源的设置

也就是我们对于长宽高的定义
px是像素
in是英寸
dp是和密度无关的像素
sp是和精度无关的像素
大家可以自行测试




<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="text_width">150px</dimen>
    <dimen name="text_height">100px</dimen>
    <dimen name="button_width">30mm</dimen>
    <dimen name="button_height">10mm</dimen>
</resources>

同样是定义这样的尺寸属性,在我们的values文件夹下。

android:width="@dimen/button_width"
android:height="@dimen/button_height"

直接使用如上所示
代码使用如下所示

 public void test3(View view){
   
   

        Button button =findViewById(R.id.button3);

        float width =this.getResources().getDimension(R.dimen.text_width);
        float height =this.getResources().getDimension(R.dimen.button_height);
        button.setWidth((int)width);
        button.setHeight((int)height);
    }

xml资源的设置

我们可以在res文件夹下定义xml文件夹,存放我们需要的xml资源
在这里插入图片描述
xml文件中存放需要的数据,例如:

<resources>
    <user username="yyj" phone ="110"></user>
    <user username="lwl" phone ="120"></user>
</resources>

具体的调用如下:

    public void test(View view) throws XmlPullParserException, IOException {
   
   
        String text ="";
        XmlResourceParser xrp=this.getResources().getXml(R.xml.users);

        while(xrp.getEventType()!=XmlResourceParser.END_DOCUMENT){
   
   
            if(xrp.getEventType()==XmlResourceParser.START_TAG){
   
   
                String tagname =xrp.getName();
                if(tagname.equals("user")){
   
   
                    String uname=xrp.getAttributeValue(0);
                    String phone=xrp.getAttributeValue(1);

                    text+="name:"+uname+";"+phone+";\n";
                }
            }
            xrp.next();
        }

        TextView textView =(TextView) findViewById(R.id.textView);
        textView.setText(text);
    }

图片资源的设置

图片资源也是存放在res中的drawable中,要特别注意图片不能以数字开头
在这里插入图片描述
具体调用代码如下:实现了按不同按钮更换背景的操作。

public void test1(View view){
   
   
        Drawable drawable=this.getResources().getDrawable(R.drawable.a1);
        this.getWindow().setBackgroundDrawable(drawable);
        //也可以在<androidx.里  android:background="@drawable/a1"设置背景

    }
    public void test2(View view){
   
   
        Drawable drawable=this.getResources().getDrawable(R.drawable.a2);
        this.getWindow().setBackgroundDrawable(drawable);


    }

以上就是所有常见资源的使用。
觉得有用的话记得点个赞呐!!
参考自:尚学堂课

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章