Chrome扩展开发指南(5)——Override Pages(重置页面)

Chrome扩展开发指南(5)——Override Pages(重置页面)

重置是一种用自己提供的页面替换Google Chrome默认页面的方法。一个重置页面常常是用HTML、JavaScript、CSS组成。

当前,能够替换的页面只有新标新标签页,新标签页就是我们打开一个新标签时出现的页面。

我们可以把默认的新标签页:

ntp-default.png


替换成这种样式:

ntp-blank.png


重置页面非常简单,只需在Manifest中定义自己的页面地址。比如下面的例子中,我们使用了newtab.html来重定义新标签页。

  1. {
  2.   "name": "My extension",
  3.   ...
  4.   "chrome_url_overrides": {
  5.     "newtab": "newtab.html"
  6.   },
  7.   ...
  8. }
复制代码


几点注意事项

为了让你定义的新标签页看起来不错,请遵循下面几点建议:

  • 保持页面简洁,使得能够快速加载
    由于新标签页经常出现,外观就显得特别重要。比如我们要避免从远程调用数据,或者读取数据库资源。
  • 确保有<title>标签
    如果没有<title>,大家讲会看到页面的URL,这会让人很迷惑,我们应该包含这样一句 <title>New Tab's Name</title>
  • 不要让键盘焦点在页面上
    我们应该让用户新建标签页的时候键盘焦点在地址栏上。
  • 不要模仿默认的新标签页面
    创建默认标签页的API(比如最近关闭的标签、最常访问的网站等等)不存在!你必须做出一些完全不同的东西。

例子

这儿examples/api/override有一些重置新标签页的例子。

其中有个我们至学习以来碰到的最简单的例子,把新标签页面换成空白页面

新建manifest.json文件:

  1. {
  2.   "name": "空白的新标签页",
  3.   "version": "0.1",
  4.   "chrome_url_overrides": {
  5.     "newtab": "blank.html"
  6.   }
  7. }
复制代码

新建文件blank.html作为默认标签页,我们可以只写这样一句话:

  1. <title>新标签页</title>
复制代码

好的,看看效果吧,就这么简单,你现在就可以动手DIY了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴为您解答关于Android开发的问题。以下是登录注册页面的示例代码: 1. activity_login.xml ``` <?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" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" android:textSize="28sp" android:textColor="@android:color/black" android:layout_gravity="center_horizontal"/> <EditText android:id="@+id/et_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:hint="Email"/> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:hint="Password" android:inputType="textPassword"/> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="Login"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="Don't have an account?" android:textColor="@android:color/black"/> <TextView android:id="@+id/tv_register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Register" android:textColor="@android:color/holo_blue_light"/> </LinearLayout> ``` 2. activity_register.xml ``` <?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" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Register" android:textSize="28sp" android:textColor="@android:color/black" android:layout_gravity="center_horizontal"/> <EditText android:id="@+id/et_fullname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:hint="Full Name"/> <EditText android:id="@+id/et_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:hint="Email"/> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:hint="Password" android:inputType="textPassword"/> <Button android:id="@+id/btn_register" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="Register"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="Already have an account?" android:textColor="@android:color/black"/> <TextView android:id="@+id/tv_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" android:textColor="@android:color/holo_blue_light"/> </LinearLayout> ``` 3. LoginActivity.java ``` public class LoginActivity extends AppCompatActivity { private EditText etEmail, etPassword; private Button btnLogin; private TextView tvRegister; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); etEmail = findViewById(R.id.et_email); etPassword = findViewById(R.id.et_password); btnLogin = findViewById(R.id.btn_login); tvRegister = findViewById(R.id.tv_register); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Perform login logic } }); tvRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(LoginActivity.this, RegisterActivity.class); startActivity(intent); } }); } } ``` 4. RegisterActivity.java ``` public class RegisterActivity extends AppCompatActivity { private EditText etFullname, etEmail, etPassword; private Button btnRegister; private TextView tvLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); etFullname = findViewById(R.id.et_fullname); etEmail = findViewById(R.id.et_email); etPassword = findViewById(R.id.et_password); btnRegister = findViewById(R.id.btn_register); tvLogin = findViewById(R.id.tv_login); btnRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Perform registration logic } }); tvLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(RegisterActivity.this, LoginActivity.class); startActivity(intent); } }); } } ``` 这个示例代码可以作为一个基础框架供您的开发使用。当然,您需要根据您的具体要求对代码进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值