• 资源管理(下)


    布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:orientation="vertical"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="helloworld.com.inspur.demo8.MainActivity">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
    
            android:id="@+id/tv"/>
        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           />
    
    </LinearLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="hah">
            <item>aaa</item>
            <item>bbb</item>
            <item>ccc</item>
            <item>ddd</item>
        </string-array>
        <integer-array name="int_arr">
            <item>111</item>
            <item>111</item>
            <item>111</item>
        </integer-array>
    </resources>

    2,逻辑

    package helloworld.com.inspur.demo8;
    
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.preference.MultiSelectListPreference;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.ScrollingTabContainerView;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.jar.Manifest;
    
    public class MainActivity extends AppCompatActivity {
      private ListView listView;
        private String[] str;
        private int[] int_arr;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView= (ListView)findViewById(R.id.lv);
            str=getResources().getStringArray(R.array.hah);
            int_arr=getResources().getIntArray(R.array.int_arr);
            ArrayAdapter myAdapter=new ArrayAdapter(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,str);
            listView.setAdapter(myAdapter);
    
        }
    }

    设置背景色(color_array)

    package helloworld.com.inspur.demo8;
    
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.res.TypedArray;
    import android.preference.MultiSelectListPreference;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.ScrollingTabContainerView;
    import android.util.Log;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.jar.Manifest;
    
    public class MainActivity extends AppCompatActivity {
      private ListView listView;
        private TextView tv1,tv2;
        private String[] str;
        private int[] int_arr;
        private TypedArray ta;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv1=(TextView)findViewById(R.id.tv1);
            tv2=(TextView)findViewById(R.id.tv2);
            listView= (ListView)findViewById(R.id.lv);
            str=getResources().getStringArray(R.array.hah);
            int_arr=getResources().getIntArray(R.array.int_arr);
            ArrayAdapter myAdapter=new ArrayAdapter(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,str);
            listView.setAdapter(myAdapter);
           
            ta=getResources().obtainTypedArray(R.array.color_arr);
            tv1.setBackgroundColor(ta.getColor(0,0));
            tv2.setBackgroundColor(ta.getColor(1,0));
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="colorPrimary">#3F51B5</color>
        <color name="colorPrimaryDark">#303F9F</color>
        <color name="colorAccent">#FF4081</color>
    </resources>
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="hah">
            <item>aaa</item>
            <item>bbb</item>
            <item>ccc</item>
            <item>ddd</item>
        </string-array>
        <integer-array name="int_arr">
            <item>111</item>
            <item>111</item>
            <item>111</item>
        </integer-array>
    
        <array name="color_arr">
            <item>@color/colorAccent</item>
            <item>@color/colorPrimaryDark</item>
            <item>@color/red</item>
        </array>
    </resources>
  • 相关阅读:
    [Java] 编写第一个java程序
    [Java] 环境变量设置
    [ActionScript 3.0] 常用的正则表达式
    [ActionScript 3.0] 正则表达式
    Python学习之==>URL编码解码&if __name__ == '__main__'
    Python学习之==>面向对象编程(一)
    Linux下安装redis-4.0.10
    Linux下编译安装Python-3.6.5
    Python学习之==>发送邮件
    Python学习之==>网络编程
  • 原文地址:https://www.cnblogs.com/excellencesy/p/9034781.html
Copyright © 2020-2023  润新知