• Android字符串,颜色,尺寸资源的使用


    字符串、颜色、尺寸资源文件
    这三种文件位于res文件夹的values文件夹中,名称分别为strings.xml , colors.xml , dimens.xml
    下面是例子,首先来看字符串资源文件strings.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="app_name">My Application123</string>
        <string name="c1">F00</string>
        <string name="c2">0F0</string>
        <string name="c3">00F</string>
            ......
    </resources>

    然后是颜色资源文件colors.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="colorPrimary">#3F51B5</color>
        <color name="colorPrimaryDark">#303F9F</color>
        <color name="colorAccent">#FF4081</color>
        <color name="c1">#F00</color>
        <color name="c2">#0F0</color>
        <color name="c3">#00F</color>
            ......
    </resources>

    以及尺寸资源文件dimens.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <dimen name="spacing">8dp</dimen>
        <dimen name="cell_width">60dp</dimen>
        <dimen name="cell_hight">66dp</dimen>
        <dimen name="title_font_size">18sp</dimen>
    </resources>


    从这几个文件可以观察到这些资源文件里定义资源的格式类似,name就是该资源的变量名称,中间就是元素的值
    <>符号中间就是资源文件类型。

    创建好资源文件后就可以使用这些资源了

    <TextView
        android:id="@+id/text01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/c1"
        android:textColor="@color/c1"
        android:textSize="@dimen/title_font_size"
        />

    从这个例子中可以看到该TextView的内容,字体颜色,字体大小都是使用资源文件而不是直接定义

    最后显示出来一个写着F00,字体颜色为红色,字体大小为18sp的TextView


    接下来是数组资源,Android并不推荐在Java代码中定义数组,因为Android允许通过资源文件定义数组资源
    来看一个例子

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <array name="plain_arr">
            <item>@color/c1</item> //该数组里的值引用了color.xml
            <item>@color/c2</item>
            <item>@color/c3</item>
            ........
        </array>
        <string-array name="string_arr">
            <item>@string/c1</item> //该数组里的值引用了string.xml
            <item>@string/c2</item>
            <item>@string/c3</item>
            ........
        </string-array>
    </resources>

    然后布局文件中创建一个GridView

    <?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"
        android:layout_gravity="center_horizontal"
        >
        <GridView
            android:id="@+id/grid01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numColumns="3"/>
    </LinearLayout>

    然后在Java代码中引用资源

    public class MainActivity extends AppCompatActivity {
        String[] texts;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //通过这种方式让texts数组得到值
            texts = getResources().getStringArray(R.array.string_arr); 
            BaseAdapter ba = new BaseAdapter() {
                @Override
                public int getCount() {
                    return texts.length;
                }
    
                @Override
                public Object getItem(int position) {
                    return texts[position];
                }
    
                @Override
                public long getItemId(int position) {
                    return position;
                }
    
                @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    TextView text = new TextView(MainActivity.this);
                    Resources res = MainActivity.this.getResources();
                    //通过这种方式设置宽和高
                    text.setWidth((int) res.getDimension(R.dimen.cell_width));
                    text.setHeight((int) res.getDimension(R.dimen.cell_hight));
                    text.setText(texts[position]);
                    //使用数组资源
                    TypedArray icons = res.obtainTypedArray(R.array.plain_arr);
                    //设置背景颜色
                    text.setBackground(icons.getDrawable(position));
                    text.setTextSize(20);
                    return  text;
                }
            };
            GridView grid = (GridView) findViewById(R.id.grid01);
            grid.setAdapter(ba);
        }
    }

    综上可以看出在XML中使用资源语法格式为:@[<package_name>:]<resource_type>/<resource_name>
    而在Java代码中使用资源的格式为:<package_name>.R.<resource_type>/<resource_name>

  • 相关阅读:
    js实现快速排序(in-place)简述
    手把手原生js简单轮播图
    基于superagent 与 cheerio 的node简单爬虫
    浅谈HTTP事务的一个过程
    css小技巧
    DesignPatternPrinciple(设计模式原则)二
    DesignPatternPrinciple(设计模式原则)一
    Attribute(特性)与AOP
    Thread(线程)四
    Thread(线程)三
  • 原文地址:https://www.cnblogs.com/vonzc/p/10459263.html
Copyright © 2020-2023  润新知