是否有如何在Android中命名资源的约定?例如,按钮,textViews,菜单等.
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的字符串值?
我正在尝试将webview的背景设置为Android中的可绘制图像资源.
从sdk看起来这样的东西可以工作,但事实并非如此.
WebView web = (WebView) findViewById(R.id.webView);
web.setBackgroundResource(R.drawable.backgroundmain);
web.loadData(profile, "text/html", "UTF-8");
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我有一个带有自定义适配器的Listview.我得到了标准
Your content must have a ListView whose id attribute is 'android.R.id.list'
Run Code Online (Sandbox Code Playgroud)
但是,错误,我的ListView有一个android:id="@android:id/list"
值集.这真的是我的头脑.任何建议?
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/Layout">
<ListView android:id="@android:id/list" style="@stlye/ListMenuView"></ListView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
styles.xml
<resources>
<style name="Layout" parent="@android:Theme">
<item name="android:background">@drawable/background</item>
<item name="android:scaleType">fitXY</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:orientation">vertical</item>
</style>
<style name="ListMenuView">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:cacheColorHint">#00000000</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
我的onCreate方法:
/** Called when the activity is first created. */
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.main );
m_menu = new ArrayList<ListItemData>(); …
Run Code Online (Sandbox Code Playgroud) 假设我有2个申请A和B.
我想从A应用程序访问B应用程序的资源(drawables,图像,字符串).我该怎么办?
编辑:
您还可以将Android项目指定为库项目,以允许它与依赖它的其他项目共享.将Android项目指定为库项目后,无法将其安装到设备上.
这是否意味着我不能在Android市场上使用我的库?我的目标是使用应用程序A的默认资源,但如果有人可以下载应用程序B并使用它资源.
我有互联网访问应用程序,不想存储string.xml
不同语言的许多文件.
我想要的是:
string.xml
带有英文字符串问题是如何string.xml
在运行时更改现有文件或添加新文件?
如果我有一个包含许多资源的大项目,那么无论如何使用目录来组织它们吗?我累了添加目录到drawable /但它没有用.
在我的应用程序中,我做一个web请求,返回一些结果代码,例如105.我有类似的字符串资源
<string name="r105">O.K.</string>
<string name="r106">Something went wrong.</string>
<string name="r333">Fatal error.</string>
Run Code Online (Sandbox Code Playgroud)
现在我想做那样的事情
Toast.makeText(parent.getApplicationContext(),
parent.getString(R.string.r+resultCode), Toast.LENGTH_LONG).show();
Run Code Online (Sandbox Code Playgroud)
while r+resultCode
是资源标识符.
这不起作用.知道怎么做吗?
我从framework-res模块中的styles.xml文件中复制此代码
Run Code Online (Sandbox Code Playgroud)<style name="Theme"> <item name="colorForeground">@android:color/bright_foreground_dark</item> <item name="colorForegroundInverse">@android:color/bright_foreground_dark_inverse</item> <item name="colorBackground">@android:color/background_dark</item>
.
Run Code Online (Sandbox Code Playgroud)<style name="Theme.Black"> <item name="android:windowBackground">@android:color/black</item> <item name="android:colorBackground">@android:color/black</item> </style>
如您所见,它们都有一个属性名称,其值为windowBackground.但是formar有一个android:
而后者没有.是否真的有必要android:
在android框架中编写前缀?
如何Bitmap
从网络(在我的意思是应用程序!)中添加一个可绘制资源?所以我可以访问Bitmap
使用R.drawable.bitmapName
...
是否有可能使用selector
drawable ImageSpan
?
我有TextView
选择器文本颜色,并添加ImageSpan与选择器资源.文本颜色更改工作正常,图像不会更改.它是一个错误还是有一种方法来制作选择器跨度?
我的TextView
:
<TextView
android:id="@+id/text"
android:ellipsize="end"
android:layout_height="fill_parent"
android:duplicateParentState="true" // parent of this text view can be selected
android:gravity="center_vertical"
android:lines="1"
android:textColor="@color/message_content_simple_text" // here is selector - it works fine
android:textSize="15dp" />
Run Code Online (Sandbox Code Playgroud)
选择:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_active" android:state_focused="true"/>
<item android:drawable="@drawable/ic_active" android:state_selected="true"/>
<item android:drawable="@drawable/ic_active" android:state_pressed="true"/>
<item android:drawable="@drawable/ic_passive"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
和添加范围的代码:
SpannableString text = new SpannableString(" some text");
ImageSpan span = new ImageSpan(context, R.drawable.icon, ImageSpan.ALIGN_BASELINE);
text.setSpan(span, 0, 1, SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(text);
Run Code Online (Sandbox Code Playgroud)
当我选择此文本视图时,"某些文本"会根据需要更改其颜色,但图像不会更改.
编辑
如果有一些android bug,你能提供任何方法来实现相同的结果吗?
我正在考虑在应用程序打包时将.txt
文件放入res/drawable
或放入res/layout
apk中,然后在安装后打开并从中读取以执行某些任务.可以这样做吗?如果是,请告诉我如何访问该文件,因为我不知道它的路径.
是否可以在android中以文本视图编写另一种语言字体,例如我创建的
TextView tv = new TextView(this);
tv.setText("HEllo Android");
Run Code Online (Sandbox Code Playgroud)
在这里,我用英语写了Hello Android,我怎么能用印地语写的呢.
我用图像工作正常
<ImageView android:id ="@+id/header2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_gravity="center"
android:paddingTop="0dip"
android:src="@drawable/why00learn00spanish_index"
>
<ImageView/>
Run Code Online (Sandbox Code Playgroud)
任何帮助都会感激.