十二、Material Design
1.Toolbar
在values/styles.xml中修改主题,把Theme.AppCompat.Light.DarkActionBar修改为Theme.AppCompat.Light.NoActionBar。
修改xml文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
</FrameLayout>
修改MainActivity文件:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
修改标题栏:
在Manifest中activity增加:android:label="Bob"
增加按钮:在res中新建menu文件夹,然后新建menu resource file:toolbar.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/backup"
android:title="Backup"
app:showAsAction="always" />
<item
android:id="@+id/delete"
android:title="Delete"
app:showAsAction="ifRoom" />
<item
android:id="@+id/settings"
android:title="Settings"
app:showAsAction="never" />
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.backup:
Toast.makeText(this,"haha",Toast.LENGTH_SHORT).show();
break;
case R.id.delete:
Toast.makeText(this,"hahaha",Toast.LENGTH_SHORT).show();
break;
case R.id.settings:
Toast.makeText(this,"heihei",Toast.LENGTH_SHORT).show();
break;
default:
}
return true;
}
2、滑动菜单
DrawerLayout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</android.support.constraint.ConstraintLayout>
<TextView
android:text="hahaha"
android:gravity="start"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.DrawerLayout>
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
//actionBar.setHomeAsUpIndicator(R.drawable.ic_launcher_background);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
break;
NavigationView:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single" >
<item android:id="nav_call"
android:title="Call" />
<item android:id="nav_friends"
android:title="Friends" />
<item android:id="@+id/nav_loction"
android:title="Location" />
</group>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/mail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
app:menu="@menu/nav_menu"
app:headerLayout="@layout/nav_header"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
NavigationView navView = (NavigationView) findViewById(R.id.nav_view);
navView.setCheckedItem(R.id.nav_call);
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
mDrawerLayout.closeDrawer();
return true;
}
});
FloatingActionButton
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:src="@mipmap/ic_launcher"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
app:elevation="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"haha",Toast.LENGTH_SHORT).show();
}
});
Snackbar:
Snackbar.make(v,"Data deleted",Snackbar.LENGTH_SHORT)
.setAction("Undo", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"haha",Toast.LENGTH_SHORT).show();
}
})
.show();
CoordinatorLayout
CardView
AppBarLayout
SwipeRefreshLayout
CollapsingtoobatLayout
利用系统状态栏空间:android:fitSystemWindows="true"
十三、开发技巧:全局获取Context、Intent传递对象、日志类
- 全局获取Context
创建Application类:
public class MyApplication extends Application{
private static Context context;
@override
public void onCreate(){
context = getApplicationContext();
}
public static Context getContext(){
return context;
}
}
在Manifest中application标签中初始化:
android:name="com.lewanjiang.test.MyApplication"
使用Context:
MyApplication.getContext()
如果使用LitePal和百度地图,这种配置过application的库时,可将MyApplication中修改:在onCreate方法中添加一句:
LitePalApplication.initialize(context);
2.用Intent传递对象
2.1 Serializable:
public class Book implements Serializable{ private String name;private String author;...}
Book book.setName("bob")...setAuthor
intent.putExtra("tran_data",book);
获取:
Book book=(Book)getIntent().getSerizlizableExtra("tran_data");
2.2 Parcelable:
public class Book implements Parcelable {
name,author...
@Override
public int describeContents() {return 0;}
@Override
public void writeToParcel(Parcel dest,int flags) {
dest.writeString(name);
dest.writeInt(author);
}
public static final Parcelable.Creator<Book> CREATOR=new Parcelable.Creator<Book>() {
@Override
public Book createFromParcel(Parcel source) {
Book person = new Book();
book.name = source.readString();
book.author = source.readInt();
return person;
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
发送和上面一样,接收变为:
getIntent().getParcelableExtra("tran_data");
3.定制日志:
public class LogUtil {
public static final int VERBOSE = 1;
public static final int DEBUG = 2;
INFO = 3;WARN=4;ERROR=5;NOTHING=6;
public static int level = VERBOSE;
public static void v(String tag,String msg){if(level<=VERBOSE) Log.v(ta,msg); }
}
4.调试
加断点
5.定时任务
Alarm机制
Doze模式
6.多窗口模式编程
7.Lambda表达式
十四、开发酷欧天气
网友评论