本文介绍了Support V7中DayNight在Activity及Dialog上的使用、AlertDialog、Dialog、AppCompatDialogFragment、AnimatedVectorDrawable、ListPopupWindow、PopupMenu、Toolbar、Buttons、Spinners、Text Input等组件的使用。
DayNight 之 Activity Usage
演示了Activity使用Theme.AppCompat.DayNight的常见用法。1
2
3
4
5
6
7
8
9
10
11
12
13
14public void setModeNightNo(View view) {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
recreate();
}
public void setModeNightYes(View view) {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
recreate();
}
public void setModeNightAuto(View view) {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
recreate();
}
DayNight 之 AlertDialog Usage
演示了AlertDialog使用Theme.AppCompat.DayNight的常见用法。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25public void setModeNightNo(View view) {
AlertDialog dialog = createAlertDialog();
dialog.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
dialog.show();
}
public void setModeNightYes(View view) {
AlertDialog dialog = createAlertDialog();
dialog.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
dialog.show();
}
public void setModeNightAuto(View view) {
AlertDialog dialog = createAlertDialog();
dialog.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
dialog.show();
}
private AlertDialog createAlertDialog() {
AlertDialog.Builder b = new AlertDialog.Builder(this,
R.style.Theme_AppCompat_DayNight_Dialog_Alert);
b.setTitle(R.string.dialog_title);
b.setMessage(R.string.dialog_content);
return b.create();
}
DayNight 之 Dialog Usage
演示了Dialog使用Theme.AppCompat.DayNight的常见用法。
1 | public void setModeNightNo(View view) { |
AlertDialog Usage
演示了AppCompat的AlertDialog常见用法。
1 | private void showSimpleDialog() { |
Dialog Usage
演示了AppCompatDialog的常见用法。
1 | private void showSimpleDialog() { |
DialogFragment Usage
演示了AppCompatDialogFragment的常见用法。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55private void showSimpleDialog() {
MenuDialogFragment fragment = MenuDialogFragment.create(R.layout.dialog_content);
fragment.show(getSupportFragmentManager(), null);
}
private void showButtonBarDialog() {
MenuDialogFragment fragment = MenuDialogFragment.create(R.layout.dialog_content_buttons);
fragment.show(getSupportFragmentManager(), null);
}
/**
* A simple {@link AppCompatDialog} implementation which
* inflates some items into it's options menu, and shows a toast when one is selected.
*/
public static class MenuDialogFragment extends AppCompatDialogFragment {
private static final String PARAM_CONTENT_VIEW = "content_view";
static MenuDialogFragment create(int contentView) {
Bundle b = new Bundle();
b.putInt(PARAM_CONTENT_VIEW, contentView);
MenuDialogFragment fragment = new MenuDialogFragment();
fragment.setArguments(b);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
Bundle args = getArguments();
int contentView = args.getInt(PARAM_CONTENT_VIEW);
return inflater.inflate(contentView, container, false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.actions, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(getActivity(), "Dialog action selected: " + item.getTitle(),
Toast.LENGTH_SHORT).show();
return true;
}
}
AnimatedVectorDrawable
1 | final ImageView imageView = (ImageView) findViewById(R.id.vector_image); |
vector_image1
2
3
4
5<ImageView android:id="@+id/vector_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:srcCompat="@drawable/animation_vector_drawable_grouping_1"/>
animation_vector_drawable_grouping_1.xml
1 | <animated-vector xmlns:android="http://schemas.android.com/apk/res/android" |
vector_drawable_grouping_1.xml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="256dp"
android:width="256dp"
android:viewportHeight="256"
android:viewportWidth="256">
<group
android:name="shape_layer_1"
android:translateX="128"
android:translateY="128">
<group android:name="sun">
<path
android:name="ellipse_path_1"
android:fillColor="#ffff8000"
android:pathData="m -25 0 a 25,25 0 1,0 50,0 a 25,25 0 1,0 -50,0"/>
<group
android:name="earth"
android:translateX="75">
<path
android:name="ellipse_path_1_1"
android:fillColor="#ff5656ea"
android:pathData="m -10 0 a 10,10 0 1,0 20,0 a 10,10 0 1,0 -20,0"/>
<group
android:name="moon"
android:translateX="25">
<path
android:name="ellipse_path_1_2"
android:fillColor="#ffadadad"
android:pathData="m -5 0 a 5,5 0 1,0 10,0 a 5,5 0 1,0 -10,0"/>
</group>
</group>
</group>
</group>
</vector>
animation_grouping_1_01.xml
1 | <set xmlns:android="http://schemas.android.com/apk/res/android"> |
List popup window
演示了List popup window的使用。
1 | private ListPopupWindow mListPopupWindow; |
Popup menu
演示了Popup menu的使用。
1 | private PopupMenu mPopupMenu; |
Toolbar Action Mode
演示了用一个Toolbar的action mode的常见用法。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?actionBarSize"
android:background="?attr/colorPrimaryDark"/>
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
private void startActionMode() {
startSupportActionMode(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.actions, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Toast.makeText(ToolbarActionMode.this,
"Action Mode item clicked:" + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
});
}
Toolbar as Action Bar
演示了Toolbar作为Action Bar的常见用法。
1 | public class ToolbarUsage extends AppCompatActivity { |
Toolbar Display Options
演示了Action Bar各个显示选项flags可以怎样被结合和它们的效果当用在Toolbar提供的Action Bar时。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92public class ToolbarDisplayOptions extends AppCompatActivity
implements View.OnClickListener {
private View mCustomView;
private ActionBar.LayoutParams mCustomViewLayoutParams;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toolbar_display_options);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
findViewById(R.id.toggle_home_as_up).setOnClickListener(this);
findViewById(R.id.toggle_show_home).setOnClickListener(this);
findViewById(R.id.toggle_use_logo).setOnClickListener(this);
findViewById(R.id.toggle_show_title).setOnClickListener(this);
findViewById(R.id.toggle_show_custom).setOnClickListener(this);
findViewById(R.id.cycle_custom_gravity).setOnClickListener(this);
findViewById(R.id.toggle_visibility).setOnClickListener(this);
// Configure several action bar elements that will be toggled by display options.
mCustomView = getLayoutInflater().inflate(R.layout.action_bar_display_options_custom, null);
mCustomViewLayoutParams = new ActionBar.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.display_options_actions, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
finish();
return true;
}
@Override
public void onClick(View v) {
final ActionBar bar = getSupportActionBar();
int flags = 0;
switch (v.getId()) {
case R.id.toggle_home_as_up:
flags = ActionBar.DISPLAY_HOME_AS_UP;
break;
case R.id.toggle_show_home:
flags = ActionBar.DISPLAY_SHOW_HOME;
break;
case R.id.toggle_use_logo:
flags = ActionBar.DISPLAY_USE_LOGO;
getSupportActionBar().setLogo(R.drawable.ic_media_play);
break;
case R.id.toggle_show_title:
flags = ActionBar.DISPLAY_SHOW_TITLE;
break;
case R.id.toggle_show_custom:
flags = ActionBar.DISPLAY_SHOW_CUSTOM;
break;
case R.id.cycle_custom_gravity: {
ActionBar.LayoutParams lp = mCustomViewLayoutParams;
int newGravity = 0;
switch (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.LEFT:
newGravity = Gravity.CENTER_HORIZONTAL;
break;
case Gravity.CENTER_HORIZONTAL:
newGravity = Gravity.RIGHT;
break;
case Gravity.RIGHT:
newGravity = Gravity.LEFT;
break;
}
lp.gravity = lp.gravity & ~Gravity.HORIZONTAL_GRAVITY_MASK | newGravity;
bar.setCustomView(mCustomView, lp);
return;
}
case R.id.toggle_visibility:
if (bar.isShowing()) {
bar.hide();
} else {
bar.show();
}
return;
}
int change = bar.getDisplayOptions() ^ flags;
bar.setDisplayOptions(change, flags);
}
}
Toolbar Fragment ViewPager
演示了ViewPager中的Fragment怎样加入到选项菜单。
1 | public class ToolbarFragmentPagerMenu extends AppCompatActivity { |
Buttons
1 | <android.support.v7.widget.SwitchCompat |
Spinners
演示了AppCompat中android.widget.Spinner控件的样式。
1 | <Spinner |
1 | // Fetch the Spinners and set an adapter |
Text Input
演示了AppCompat中文本输入控件的样式,例如android.widget.EditText,android.widget.AutoCompleteTextView和android.widget.MultiAutoCompleteTextView。
1 | // Fetch the AutoCompleteTextView and set an adapter |
从Android SDK目录可找到源码,或从这里下载