1. Activity 类
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.EditText;
public class LoginActivity extends AppCompatActivity{
private Toolbar loginToolbar;
private EditText userNameEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
userNameEditor = (EditText) findViewById(R.id.userName);
loginToolbar = (Toolbar) findViewById(R.id.LoginToolbar);
setSupportActionBar(loginToolbar);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
}
2. LayoutXml 文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/LoginToolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="@color/colorBlue">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="22dp"
android:textColor="@color/colorWhite"
android:text="@string/title_activity_login"/>
</android.support.v7.widget.Toolbar>
<EditText
android:id="@+id/userName"
android:hint="请输入用户名"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"/>
<EditText
android:id="@+id/userPassword"
android:hint="请输入用户密码"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"/>
</LinearLayout>
3. AndroidManifest.xml
<activity android:name=".LoginActivity" android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
4. Style.xml (在AndroidManifest.xml 中被使用)
使用 V7 toolbar 要使用Theme.AppCompat.Light.NoActionBar, 一定要用NoActionBar
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">@color/colorWhite</item>
<item name="colorAccent">@color/colorRed</item>
</style>