在Material Design中,尤其是在appcompat-V7里面有很多为兼容而生的控件这样就可以做到高低版本和不同的ROM之间体验一致!还可以配合appcompat的主题使用达到体验一致性
例如:
1.android.support.v7.app.AlertDialog
2.进度条样式设置
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
3.SwipeRefreshLayout下拉刷新
4.PopupWindow
ListPopupWindow
PopupMenu
....
5.android.support.v7.widget.LinearLayoutCompat
好了!开始上代码
首先引入依赖:
compile 'com.android.support:appcompat-v7:23.4.0'
activity_main.xml:
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/srl"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
app:divider="@drawable/abc_list_divider_mtrl_alpha"
app:showDividers="beginning|middle"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="23dp"
android:text="对话框" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="23dp"
android:onClick="showPopup"
android:text="泡泡窗口ListPopupWindow" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="23dp"
android:onClick="showPopupMenu"
android:text="PopupMenu" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="23dp"
android:text="呵呵" />
<android.support.v7.widget.AppCompatEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="23dp"
android:text="呵呵" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="27dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<ProgressBar
android:id="@+id/progressBar1"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="65dp" />
</android.support.v7.widget.LinearLayoutCompat>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
main
.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.ricky.md.theme.MainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:icon="@android:drawable/ic_menu_set_as"
android:title="设置"/>
<item
android:id="@+id/action_share"
android:orderInCategory="101"
android:icon="@android:drawable/ic_menu_share"
android:title="分享"/>
<item
android:id="@+id/action_new"
android:orderInCategory="102"
android:icon="@android:drawable/ic_menu_add"
android:title="添加"/>
</menu>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar1;
private SwipeRefreshLayout srl;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatButton appCompatButton = (AppCompatButton) findViewById(R.id.button1);
appCompatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
}
});
progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
progressBar1.setMax(100);
progressBar1.setProgress(50);
srl = (SwipeRefreshLayout) findViewById(R.id.srl);
srl.setSize(SwipeRefreshLayout.LARGE);
srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// 下拉完毕 加载更多数据
//srl.setRefreshing(false);
}
});
srl.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
//设置进度条的背景颜色
srl.setProgressBackgroundColorSchemeColor(Color.YELLOW);
//设置下拉多少距离开始刷新
//srl.setDistanceToTriggerSync(70);
String[] items = {"条目0", "条目1", "条目2", "条目3", "条目4", "条目5", "条目6",};
//数据
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
}
public void showPopupMenu(View v) {
PopupMenu popupMenu = new PopupMenu(this, v);
popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu());
//popupMenu.setOnMenuItemClickListener(listener);
popupMenu.show();
}
public void showPopup(View v) {
final ListPopupWindow listPopupWindow = new ListPopupWindow(this);
listPopupWindow.setAdapter(adapter);
//设置锚点,弹出的位置是相对于v的位置
listPopupWindow.setAnchorView(v);
listPopupWindow.setWidth(200);
listPopupWindow.setHeight(500);
listPopupWindow.show();
listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "点了第" + position, Toast.LENGTH_SHORT).show();
listPopupWindow.dismiss();
}
});
}
public void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("女朋友");
builder.setMessage("给我一个女朋友");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.show();
}
}
效果图:
注:
这其中导入的包,都是在appcompat_v7中,除了progressBar,其他的控件都可以达到在4.0到5.0+以上手机的体验相同.
除了上述几个控件之外,appCompat-v7中还有许多其他控件。这里的控件都是属于兼容控件,是为了做到在不同系统手机上的体验达到一致,不包括Material Design新增控件
AppCompat源码下载地址:源码地址
大家可以自行研究。