1、获取资源: Resources res = getBaseContext().getResources();
Drawable draw=res.getDrawable(R.drawable.icon);
2、获得数组: String[] ary = getResources().getStringArray(R.array.ary);
3、自动提示框:
String[] arrays=new String[]{"a","ab","abc","bc","bcde","ee"};
actalert=(AutoCompleteTextView)findViewById(R.id.actalert);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(
this,
android.R.layout.simple_dropdown_item_1line,
arrays);
actalert.setAdapter(adapter);
mactalert=(MultiAutoCompleteTextView)findViewById(R.id.mactvalert);
mactalert.setAdapter(adapter);
// 设置多个值之间的分隔符,此处为逗号
mactalert.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
4、spinner设置数据源
spncolor.setPrompt("请选择");
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.attr, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spncolor.setAdapter(adapter);
5、显示当前时间:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
// 将当前时间显示在TextView组件中
tvTime.setText("当前时间:" + sdf.format(new Date()));
6、代码添加布局文件
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView text = new TextView(activity);
text.setLayoutParams(lp);
text.setTextSize(20);
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
text.setPadding(36, 0, 0, 0);
text.setText(s);
7、自定义窗口标题栏代码
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);//自定义标题栏
setContentView(R.layout.main);
//为标题栏设置一个xml布局
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_custom);
custom_title.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/tvtitle"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentLeft="true"></TextView>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tvtile" android:orientation="horizontal"
android:layout_alignParentRight="true" android:gravity="center">
<Button android:text="@string/answer" android:id="@+id/btnanswer"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@color/btn_inter" android:layout_marginRight="8dp"
android:gravity="center" android:paddingTop="2dp"></Button>
<Button android:text="@string/submit" android:id="@+id/btnsubmit"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@color/btn_inter" android:layout_marginRight="8dp"
android:gravity="center" android:paddingTop="2dp"></Button>
</LinearLayout>
</RelativeLayout>