- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在表格布局正方形中制作 4 个按钮。
按钮看起来像那样(id 数到 4):
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:background="@drawable/tile_selector"
android:text="Button1" />
到目前为止一切看起来都很好。现在我想获取每个 Button 的 witdh 并将该值设置为其高度。
我的代码:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
for (int i = 0; i <= 4; i++) {
String buttonID = "button" + i;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
Button b = (Button) findViewById(resID);
int width = b.getWidth();
b.setHeight(width);
}
}
但是那会崩溃……为什么?
请帮帮我。
最佳答案
试着让你的按钮如下所示
Button[] buttons;
for(int i=0; i<4; i++) {
{
String buttonID = "button" + (i+1);
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i] = ((Button) findViewById(resID));
//set your height and width as you are doing.
}
希望它有用...
关于android - 使用 getResources().getIdentifier 对 4 个按钮进行平方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15535471/
我真的不知道为什么,但我无法检索字符串的 Id。 这是我的代码: int resId = getApplicationContext().getResources() .getI
我试过了: r = Resources.getSystem().getIdentifier("ball_red","drawable","com.Juggle2"); Log.i("FindBall"
我的 XML 中有以下元素 我正在尝试在代码中检索它的值,例如: String name = "kernelb11"; int a = this.getR
我的代码或信标返回“错误”值(用引号引起来,因为这很可能是我的代码中的错误)有问题。我一直在阅读信标,据我所知,我可以给我的信标 3 个标识符。我已将信标的标识符配置为 000000000000001
我需要显示保存在 res/drawable 文件夹下的图像。我从 Web 服务调用中获取图像名称,并尝试使用以下代码在可绘制文件夹中找到该图像 ImageView backgroundImage =
在一个 Activity 中,有 1 个 TextView 和 1 个 ImageView 。我必须显示不同图像的数据数量。我已将数据存储在 strings.xml 中的字符串数组中。我的代码对他们来
我试图在卡片 View 中显示图像和标题。代码应该从文本文件中获取图像名称并将其转换为图像的 ID,这些图像已经在可绘制文件中。但是当我在模拟器上运行我的代码时,只显示标题。所以我使用 Log.d 检
在Android SDK中,有一条评论说通过标识符检索资源比通过名称检索资源效率更高。 这是不鼓励使用 getIdentifier() 的唯一原因吗? 我需要以编程方式访问数百个资源中的一个,到目前为
如何从 R. 获取 id 的 int id 值?当我使用 getIdentifier 时,它只返回 0。 int i = getArguments().getInt(SELECTION_NUMBER
我正在尝试从 strings.xml 中获取值为“Hello”的字符串的 ID,执行以下操作: getContext().getResources().getIdentifier("Hello", "
我正在使用 OpenGL ES 在 Android 中制作游戏。我从教程中获得了一些代码,我正在尝试更改它以适合我的应用程序,但我遇到了问题。我想使用传递到函数中的字符串作为资源名称来动态获取图像资源
我正在循环访问一个数组,在该 for 循环中我还使用该数组来获取几个微调器的 ID: Integer[] ids; ids = new Integer[8]; ids[0] = R.id.nr_are
我正在尝试使用以下代码获取图像的 id。 public class MovieActivity extends ActionBarActivity { private Context
我目前正在开发一个音乐播放器应用程序,我的可绘制文件夹中有一些图像需要按特定顺序显示。我发现我必须使用 getResources().getIdentifier() 方法,但我的所有歌曲总是得到相同的
我通过 json 获取我的数据。将其放入 Hashmap 并在 ListView 中列出。效果很好! 现在我想要这样的东西: String myArt=report_TJ_+e.getString("
我使用 Resources.getIdentifier 动态加载字符串资源;因为我动态传递了一个需要从我的资源文件中翻译的字符串。资源不存在时返回0;所以我处理。但是,当我传入一个数字字符串时;即使我
我有一个包含以下资源的 xml 文件 等等 我有以下代码: String id = "t" + c.get(Calendar.DAY_OF_WEEK) + c.get(Calend
我有这样的代码: String s = "replace__menu__" + data.imageid + ".png"; int RID = this.getApplicationContext(
我正在尝试了解一款对投资组合进行计算的 Android 应用。投资组合存储在 res/values/portfolio.xml 中: 当在应用程序中按下一个按钮时,投资组合数据被检索如下: Strin
我无法让 getIdentifier 处理类变量。这很奇怪,因为这有效: public Drawable getStationIcon(Context context) { int resId
我是一名优秀的程序员,十分优秀!