Android的神奇之处在于只通过R.id.XXX找到合适的资源.
AFAIK,资源被编译成二进制格式,那么这个映射逻辑如何在底层工作呢?
也许它的工作原理如下:
例如,在layout1.xml中,我们得到:
<Button android:id="@+id/button1" >
Run Code Online (Sandbox Code Playgroud)
和AAPT将在R.java中生成:
public static final int button1=0x7f05000b;
Run Code Online (Sandbox Code Playgroud)
当生成*.apk时,@ + id/button1将替换为"0x7f05000b".
因此,当我们打电话时:
findViewById(R.id.button1);
Run Code Online (Sandbox Code Playgroud)
我们基本上仍然根据ID进行搜索,尽管ID是一个像0x7f05000b的数字.
谢谢!
我真正想知道的是,如何将资源id整数解析为资源内容?换句话说,Android运行时如何以资源ID作为唯一线索来定位资源内容?
例如,如何找到具有资源ID的可绘制图片?或者如何找到具有资源ID的字符串值?
我想使用NumberPicker组件小部件但是在默认的Holo主题中,我需要用橙色替换蓝色,因为这是我造型中的默认颜色.如何更换蓝色和数字的颜色,并保留组件的所有功能?
谢谢
我正在尝试使用 Jetpack 导航组件。这里的文档讨论了动画过渡。示例代码使用动画slide_in_right和 ,slide_out_left其行为就像它们默认存在一样 - 没有关于如何创建它们的说明。
<action
...
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
Run Code Online (Sandbox Code Playgroud)
但是,当我单击如下所示的属性时,在导航图资源的设计视图中,我只看到slide_in_left和side_out_right。为什么另外两个人不在?
我的目标是制作类似推/弹出的动画,其中新视图从右侧进入,旧视图从左侧移出。(相反,“弹出”回到导航堆栈中。)
我确实看到了一些关于这些动画的其他问题,但他们的答案很旧,而且听起来可能存在错误,所以我想知道 2020 年现在的答案是什么。
在我的项目中,我需要为视图设置始终常量ID.我的意思是不同应用程序版本之间的ID是不变的.经过一些调查后,我发现它可以通过使用来实现,values/public.xml并且该文件中声明的任何id在将来的构建中都不会改变.现在的问题是我无法在某些布局文件中定义视图的id.这是我的layout.xml,其中包含一个带有Id的ImageView,应该将其添加到public.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/menu_item_selector" >
<ImageView
android:id="@+id/index_row_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="@dimen/padding_small" />
<ImageView
android:id="@+id/index_row_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/index_row_search" />
<TextView
android:id="@+id/index_row_caption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/index_row_icon"
android:layout_toRightOf="@id/index_row_search"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceLarge" />
Run Code Online (Sandbox Code Playgroud)
这是public.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<public type="string" name="no_internet" id="0xAA0a0001" />
<public type="id" name="index_row_search" id="0xAA0b0015" />
</resources>
Run Code Online (Sandbox Code Playgroud)
eclipse在我添加id"index_row_search"的行上显示错误,并说:
error: Public symbol id/index_row_search declared here is not defined!
但正如您在上面的布局文件中看到的,我有一个带有该ID的ImageView.它想知道上面一行的字符串id定义没有错误!
那么,我应该如何在public.xml中定义View的Id?