工具的真正目的是什么:Android XML 中的上下文 [英] What is the real purpose of tools:context in Android XML

查看:25
本文介绍了工具的真正目的是什么:Android XML 中的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 android 并想知道为什么我在布局 XML 文件中找不到属性 tools:context 的真正用途.

I'm learning android and wonder why I can't find the real purpose of the attribute tools:context in layout XML file.

我在这里阅读 https://developer.android.com/studio/write/tool-attributes.html#toolscontext 在我们指定 tools:context 之前 onClick 不适用于任何视图的图像下.

I read here https://developer.android.com/studio/write/tool-attributes.html#toolscontext under the image that onClick will not work for any View until we specify tools:context.

我试过,想知道它在没有任何 tools:context 的情况下也能工作我还从 stackoverflow 中读到它曾经为布局选择合适的主题.但是它在不使用 tools:context 的情况下对我来说很好,那么它的真正目的是什么?

I tried and wonder it is working without any tools:context I also read from the stackoverflow that it used to choose proper theme for the layout. But it is working fine for me without using tools:context then what is the real purpose of this?

XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="CallIt"
        />
</RelativeLayout>

主要活动:

package com.something.testingwith42;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void CallIt(View view){
        Toast.makeText(this, "Checking", Toast.LENGTH_LONG).show();
    }
}

没有 tools:context

推荐答案

在我们指定 tools:context 之前,onClick 不适用于任何视图

onClick will not work for any View until we specify tools:context

该文档中没有任何内容可以做出这种声明.

There is nothing in that documentation that makes that claim.

在没有工具的情况下一切正常:上下文

Everything is working fine without tools:context

正确.那是因为该文档中没有任何内容声明 onClick() 仅在您拥有 tools:context 时才有效.确实说,为了快速修复,您需要 tools:context 让工具知道在哪里添加 onClick() 方法.

Correct. That is because that is nothing in that documentation that states that onClick() will only work if you have tools:context. It does say that for quick-fixes, you need tools:context for the tools to know where to add the onClick() method.

还从 stackoverflow 中读取它用于为布局选择合适的主题

also read from the stackoverflow that it used to choose proper theme for the layout

这也是错误的.

工具的真正目的是什么:Android XML 中的上下文

What is the real purpose of tools:context in Android XML

它为开发工具提供了上下文,关于此布局将在何处使用,以便工具可以为开发人员提供更好的帮助,例如:

It provides context for development tools, as to where this layout will be used, so that the tools can provide better assistance to developers, such as:

  • 更准确地呈现预览,将托管活动及其主题考虑在内
  • 更智能的助手,例如前面提到的快速修复

tools:context 是完全可选的.开发工具没有它也能工作.这些工具可能为您提供的帮助有所下降,但这是您的选择.

tools:context is completely optional. Development tools can and do work without it. Those tools may provide somewhat degraded assistance to you, but that is your choice.

tools:context —或 tools 命名空间中的任何属性 —在运行时不会有任何影响.我还没有检查过,但我希望它们在打包 APK 时被剥离,因为它们在运行时没有用.

tools:context — or any attribute in the tools namespace — will have no impact at runtime. I have not checked, but I would hope that they even get stripped out when packaging the APK, as they have no use at runtime.

这篇关于工具的真正目的是什么:Android XML 中的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆