布局
<?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>