Java 如何以编程方式访问嵌套布局中的视图?
我试图在java代码中使用Java 如何以编程方式访问嵌套布局中的视图?,java,android,xml,android-layout,Java,Android,Xml,Android Layout,我试图在java代码中使用ImageView,它位于XML中第二个RelativeLayout节点内。但是,我无法通过不使图像不可见的setVisibility方法获得对ImageViews的引用 [DOUBLE EDIT]这是我修订的活动课: package com.scopelyapplication.tictactoe; import android.content.Intent; import android.os.Bundle; import android.support.v7.a
ImageView
,它位于XML中第二个RelativeLayout
节点内。但是,我无法通过不使图像不可见的setVisibility
方法获得对ImageViews
的引用
[DOUBLE EDIT]这是我修订的活动课:
package com.scopelyapplication.tictactoe;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class TicTacToeActivity extends ActionBarActivity {
private static final String LOGTAG = "TicTacToeActivity";
private ModelComputerGameHelper comHelp;
boolean humanIsX;
RelativeLayout layout;
ImageView blueX;
ImageView greyX;
ImageView blueO;
ImageView greyO;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (RelativeLayout)findViewById(R.id.mainLayout);
Log.d(LOGTAG, "onCreate");
//Set up action bar
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
Log.d(LOGTAG, "set action bar");
//Initialize X's and O's
blueX = (ImageView)findViewById(R.id.blueXTopLeft);
greyX = (ImageView)findViewById(R.id.greyXTopLeft);
blueO = (ImageView)findViewById(R.id.blueOTopLeft);
greyO = (ImageView)findViewById(R.id.greyOTopLeft);
Log.d(LOGTAG, "X's and O's initialized");
//Turn off their visibility until one is clicked.
blueX.setVisibility(View.INVISIBLE); //where the NPE occurs
greyX.setVisibility(View.INVISIBLE);
blueO.setVisibility(View.INVISIBLE);
greyO.setVisibility(View.INVISIBLE);
Bundle extras = getIntent().getExtras();
boolean gameIsPVP = extras.getBoolean("gameIsPVP");
humanIsX = extras.getBoolean("humanIsX");
if (gameIsPVP) {
; //in progress
} else {
startComputerGame(humanIsX);
}
}
public void squareOneClick(View imageView) {
char humanPlayer = ' ';
if (humanIsX) {
humanPlayer = 'X';
} else {
humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 0);
if (humanPlayer == 'X') {
blueX.setVisibility(View.VISIBLE);
} else {
blueO.setVisibility(View.VISIBLE);
}
updateBoard();
}
public void squareTwoClick(View imageView) {
char humanPlayer = ' ';
if (humanIsX) {
humanPlayer = 'X';
}
else {
humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 1);
updateBoard();
}
public void squareThreeClick(View imageView) {
char humanPlayer = ' ';
if (humanIsX) {
humanPlayer = 'X';
}
else {
humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 2);
updateBoard();
}
public void squareFourClick(View imageView) {
char humanPlayer = ' ';
if (humanIsX) {
humanPlayer = 'X';
}
else {
humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 3);
updateBoard();
}
public void squareFiveClick(View imageView) {
char humanPlayer = ' ';
if (humanIsX) {
humanPlayer = 'X';
}
else {
humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 4);
updateBoard();
}
public void squareSixClick(View imageView) {
char humanPlayer = ' ';
if (humanIsX) {
humanPlayer = 'X';
}
else {
humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 5);
updateBoard();
}
public void squareSevenClick(View imageView) {
char humanPlayer = ' ';
if (humanIsX) {
humanPlayer = 'X';
}
else {
humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 6);
updateBoard();
}
public void squareEightClick(View imageView) {
char humanPlayer = ' ';
if (humanIsX) {
humanPlayer = 'X';
}
else {
humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 7);
updateBoard();
}
public void squareNineClick(View imageView) {
char humanPlayer = ' ';
if (humanIsX) {
humanPlayer = 'X';
}
else {
humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 8);
updateBoard();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
startActivity(new Intent(this, PickerActivity.class));
}
return super.onOptionsItemSelected(item);
}
public void startComputerGame(boolean isHumanX) {
Log.d(LOGTAG, "startComputerGame");
comHelp = new ModelComputerGameHelper(isHumanX);
updateBoard();
}
public void updateBoard() {
//in progress. connects startComputerGame and Clicks with Visuals.
}
}
下面是我在活动\u main中的一些XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="20dp"
android:minWidth="20dp" >
<ImageView
android:id="@+id/gameBoard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/gameboard"
android:src="@drawable/gameboard" />
<RelativeLayout
android:id="@+id/squareOneLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="290dp"
android:layout_marginRight="220dp"
android:layout_marginTop="70dp" >
<ImageButton
android:id="@+id/squareOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="288dp"
android:layout_marginRight="219dp"
android:layout_marginTop="71dp"
android:background="@android:color/transparent"
android:contentDescription="@string/top_left"
android:onClick="squareOneClick"
android:src="@drawable/blank_button" />
<!-- Top-left images -->
<ImageView
android:id="@+id/blueOTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/greyOTopLeft"
android:layout_centerVertical="true"
android:src="@drawable/piece_o" />
<ImageView
android:id="@+id/greyOTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/piece_o_grey" />
<ImageView
android:id="@+id/greyXTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/blueOTopLeft"
android:layout_centerVertical="true"
android:src="@drawable/piece_x_grey" />
<ImageView
android:id="@+id/blueXTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/blueOTopLeft"
android:layout_centerVertical="true"
android:src="@drawable/piece_x" />
</RelativeLayout>
... <!-- rest of xml -->
</RelativeLayout>
谢谢大家! 只需使用其ID直接访问即可。
将行更改为:
blueX = (ImageView) findViewById(R.id.blueXTopLeft);
然后对其他相关变量执行类似操作,它就会工作。只需使用ID直接访问它。
将行更改为:
blueX = (ImageView) findViewById(R.id.blueXTopLeft);
然后对其他相关变量执行类似操作,它就会工作。只需使用ID直接访问它。
将行更改为:
blueX = (ImageView) findViewById(R.id.blueXTopLeft);
然后对其他相关变量执行类似操作,它就会工作。只需使用ID直接访问它。
将行更改为:
blueX = (ImageView) findViewById(R.id.blueXTopLeft);
然后对其他相关变量执行类似的操作,它就会起作用。如前所述,您是否尝试过对项目进行清理?您的R.java可能有点混乱。项目>清理。如前所述,您是否尝试过对项目进行清理?您的R.java可能有点混乱。项目>清理。如前所述,您是否尝试过对项目进行清理?您的R.java可能有点混乱。项目>清理。如前所述,您是否尝试过对项目进行清理?您的R.java可能有点混乱。项目>清洁。试试这个 活动\u main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="20dp"
android:minWidth="20dp" >
<ImageView
android:id="@+id/gameBoard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/gameboard"
android:src="@drawable/gameboard" />
<RelativeLayout
android:id="@+id/squareOneLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="290dp"
android:layout_marginRight="220dp"
android:layout_marginTop="70dp" >
<ImageButton
android:id="@+id/squareOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="288dp"
android:layout_marginRight="219dp"
android:layout_marginTop="71dp"
android:background="@android:color/transparent"
android:contentDescription="@string/top_left"
android:onClick="squareOneClick"
android:src="@drawable/blank_button" />
<!-- Top-left images -->
<ImageView
android:id="@+id/blueOTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/greyOTopLeft"
android:layout_centerVertical="true"
android:src="@drawable/piece_o"
android:visibility="invisible"/>
<ImageView
android:id="@+id/greyOTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/piece_o_grey"
android:visibility="invisible"/>
<ImageView
android:id="@+id/greyXTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/blueOTopLeft"
android:layout_centerVertical="true"
android:src="@drawable/piece_x_grey"
android:visibility="invisible"
/>
<ImageView
android:id="@+id/blueXTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/blueOTopLeft"
android:layout_centerVertical="true"
android:src="@drawable/piece_x"
android:visibility="invisible"/>
</RelativeLayout>
... <!-- rest of xml -->
</RelativeLayout>
试试这个
活动\u main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="20dp"
android:minWidth="20dp" >
<ImageView
android:id="@+id/gameBoard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/gameboard"
android:src="@drawable/gameboard" />
<RelativeLayout
android:id="@+id/squareOneLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="290dp"
android:layout_marginRight="220dp"
android:layout_marginTop="70dp" >
<ImageButton
android:id="@+id/squareOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="288dp"
android:layout_marginRight="219dp"
android:layout_marginTop="71dp"
android:background="@android:color/transparent"
android:contentDescription="@string/top_left"
android:onClick="squareOneClick"
android:src="@drawable/blank_button" />
<!-- Top-left images -->
<ImageView
android:id="@+id/blueOTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/greyOTopLeft"
android:layout_centerVertical="true"
android:src="@drawable/piece_o"
android:visibility="invisible"/>
<ImageView
android:id="@+id/greyOTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/piece_o_grey"
android:visibility="invisible"/>
<ImageView
android:id="@+id/greyXTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/blueOTopLeft"
android:layout_centerVertical="true"
android:src="@drawable/piece_x_grey"
android:visibility="invisible"
/>
<ImageView
android:id="@+id/blueXTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/blueOTopLeft"
android:layout_centerVertical="true"
android:src="@drawable/piece_x"
android:visibility="invisible"/>
</RelativeLayout>
... <!-- rest of xml -->
</RelativeLayout>
试试这个
活动\u main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="20dp"
android:minWidth="20dp" >
<ImageView
android:id="@+id/gameBoard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/gameboard"
android:src="@drawable/gameboard" />
<RelativeLayout
android:id="@+id/squareOneLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="290dp"
android:layout_marginRight="220dp"
android:layout_marginTop="70dp" >
<ImageButton
android:id="@+id/squareOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="288dp"
android:layout_marginRight="219dp"
android:layout_marginTop="71dp"
android:background="@android:color/transparent"
android:contentDescription="@string/top_left"
android:onClick="squareOneClick"
android:src="@drawable/blank_button" />
<!-- Top-left images -->
<ImageView
android:id="@+id/blueOTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/greyOTopLeft"
android:layout_centerVertical="true"
android:src="@drawable/piece_o"
android:visibility="invisible"/>
<ImageView
android:id="@+id/greyOTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/piece_o_grey"
android:visibility="invisible"/>
<ImageView
android:id="@+id/greyXTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/blueOTopLeft"
android:layout_centerVertical="true"
android:src="@drawable/piece_x_grey"
android:visibility="invisible"
/>
<ImageView
android:id="@+id/blueXTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/blueOTopLeft"
android:layout_centerVertical="true"
android:src="@drawable/piece_x"
android:visibility="invisible"/>
</RelativeLayout>
... <!-- rest of xml -->
</RelativeLayout>
试试这个
活动\u main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="20dp"
android:minWidth="20dp" >
<ImageView
android:id="@+id/gameBoard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/gameboard"
android:src="@drawable/gameboard" />
<RelativeLayout
android:id="@+id/squareOneLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="290dp"
android:layout_marginRight="220dp"
android:layout_marginTop="70dp" >
<ImageButton
android:id="@+id/squareOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="288dp"
android:layout_marginRight="219dp"
android:layout_marginTop="71dp"
android:background="@android:color/transparent"
android:contentDescription="@string/top_left"
android:onClick="squareOneClick"
android:src="@drawable/blank_button" />
<!-- Top-left images -->
<ImageView
android:id="@+id/blueOTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/greyOTopLeft"
android:layout_centerVertical="true"
android:src="@drawable/piece_o"
android:visibility="invisible"/>
<ImageView
android:id="@+id/greyOTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/piece_o_grey"
android:visibility="invisible"/>
<ImageView
android:id="@+id/greyXTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/blueOTopLeft"
android:layout_centerVertical="true"
android:src="@drawable/piece_x_grey"
android:visibility="invisible"
/>
<ImageView
android:id="@+id/blueXTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/blueOTopLeft"
android:layout_centerVertical="true"
android:src="@drawable/piece_x"
android:visibility="invisible"/>
</RelativeLayout>
... <!-- rest of xml -->
</RelativeLayout>
我认为您的问题在于布局XML。请参阅文档,其中说明: 请注意,RelativeLayout的大小与其子对象的位置之间不能存在循环依赖关系。例如,不能将高度设置为“包裹内容”的RelativeLayout和将子项设置为“对齐父项”的RelativeLayout 您需要纠正squareOneLayout中的循环依赖关系(可能还有其他地方) 如果希望子对象相对于父对象RelativeLayout进行定位,则RelativeLayout无法根据其子对象的大小调整自身大小 换句话说,如果子对象的位置相对于父对象(RelativeLayout)的大小,则父对象(RelativeLayout)无法根据其子对象(ImageView)的大小和总体布局调整自身大小。结果是无法解析的循环依赖关系
您需要更改RelativeLayout的大小,或者将ImageView相对于同级视图(例如其中一个ImageView)定位,而不是相对于父RelativeLayout定位。我认为您的问题在于布局XML。请参阅文档,其中说明: 请注意,RelativeLayout的大小与其子对象的位置之间不能存在循环依赖关系。例如,不能将高度设置为“包裹内容”的RelativeLayout和将子项设置为“对齐父项”的RelativeLayout 您需要纠正squareOneLayout中的循环依赖关系(可能还有其他地方) 如果希望子对象相对于父对象RelativeLayout进行定位,则RelativeLayout无法根据其子对象的大小调整自身大小 换句话说,如果子对象的位置相对于父对象(RelativeLayout)的大小,则父对象(RelativeLayout)无法根据其子对象(ImageView)的大小和总体布局调整自身大小。结果是无法解析的循环依赖关系
您需要更改RelativeLayout的大小,或者将ImageView相对于同级视图(例如其中一个ImageView)定位,而不是相对于父RelativeLayout定位。我认为您的问题在于布局XML。请参阅文档,其中说明: 请注意,RelativeLayout的大小与其子对象的位置之间不能存在循环依赖关系。例如,不能将高度设置为“包裹内容”的RelativeLayout和将子项设置为“对齐父项”的RelativeLayout 您需要纠正squareOneLayout中的循环依赖关系(可能还有其他地方) 如果希望子对象相对于父对象RelativeLayout进行定位,则RelativeLayout无法根据其子对象的大小调整自身大小 换句话说,如果子对象的位置相对于父对象(RelativeLayout)的大小,则父对象(RelativeLayout)无法根据其子对象(ImageView)的大小和总体布局调整自身大小。结果是无法解析的循环依赖关系
您需要更改RelativeLayout的大小,或者将ImageView相对于同级视图(例如其中一个ImageView)定位,而不是相对于父RelativeLayout定位。我认为您的问题在于布局XML。请参阅文档,其中说明: 请注意,RelativeLayout的大小与其子对象的位置之间不能存在循环依赖关系。例如,不能将高度设置为“包裹内容”的RelativeLayout和将子项设置为“对齐父项”的RelativeLayout 您需要纠正squareOneLayout中的循环依赖关系(可能还有其他地方) 如果希望子对象相对于父对象RelativeLayout进行定位,则RelativeLayout无法根据其子对象的大小调整自身大小 换句话说,如果子对象(ImageView)已定位,则父对象(RelativeLayout)无法根据其子对象(ImageView)的大小和总体布局调整自身大小