【发布时间】:2015-06-05 15:21:04
【问题描述】:
我正在尝试以编程方式启用和禁用 4 个 UI 按钮。我正在使用 Unity3D,但我似乎无法让它工作。我错过了什么?我当前的尝试如下所示:
我的LinearLayoutxml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical" >
<com.BoostAR.Generic.TintedImageButton
android:id="@+id/helpButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/overlayButtonMargin"
android:src="@drawable/help"
android:visibility="visible" />
<com.BoostAR.Generic.TintedImageButton
android:id="@+id/refreshButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/overlayButtonMargin"
android:src="@drawable/refresh" />
<com.BoostAR.Generic.TintedImageButton
android:id="@+id/screenshotButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/overlayButtonMargin"
android:src="@drawable/photo" />
<com.BoostAR.Generic.LockButton
android:id="@+id/lockButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/overlayButtonMargin"
android:src="@drawable/unlocked" />
</LinearLayout>
我在代码中做了什么:
private static final int[] AUGMENTED_UI_IDS = {
R.id.refreshButton, R.id.screenshotButton, R.id.lockButton
};
private void updateAugmentedUiVisibility()
{
final int visibility =
(mShouldShowAugmentedUI ? View.VISIBLE : View.INVISIBLE);
runOnUiThread(new Runnable() {
@Override
public void run() {
for (int id : AUGMENTED_UI_IDS) {
final View view = findViewById(id);
if (view == null) {
Log.e(LOG_TAG, "Failed to find view with ID: " + id);
} else {
Log.e(LOG_TAG, "Visibility: " + visibility);
view.setVisibility(visibility);
}
}
}
});
}
}
结果:
声明
Log.e(LOG_TAG, "Failed to find view with ID: " + id);
被调用。当我确实交叉引用了似乎很好的身份证号码时。
【问题讨论】:
-
您应该在此处粘贴相关代码。 “打开和关闭这 4 个 UI 按钮”到底是什么意思?您想禁用/启用它们吗?
-
我确实添加了这个问题。谢谢,是的,我想禁用和启用它们。这就是我想做的。
-
已编辑以尝试改进您的格式和语法。我还删除了无关的介绍材料和谢谢。另外,我从 pastebin 导入了 xml(请不要将您的部分问题链接到第 3 方网站)。
-
您是否在任何时候在调用
updateAugmentedUiVisibility调用setContentView(that-layout-id)或将所述布局以任何其他方式膨胀到视图层次结构中以便findViewById可以找到它?
标签: java android xml user-interface