为什么只有我的 xml 布局有效并且优先于 OnCreate 中的代码?我正在尝试使图像视图可见/不可见,但只有布局可以使它,你知道为什么吗?
它只是一个垂直的线性布局:
<ImageView
android:id="@+id/friendsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:src="@drawable/friends_button" />
整个xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="86dp"
android:layout_marginTop="10dp"
android:orientation="vertical"
>
<TextView
android:id="@+id/textRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play_button" />
<ImageView
android:id="@+id/friendsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:src="@drawable/friends_button" />
</LinearLayout>
</RelativeLayout>
并且friendsButton.setVisibility(View.INVISIBLE);
在OnCreate
我试图把它在布局中不可见和在代码中可见,但仍然与布局相同的优先级......
任何想法?
谢谢
编辑 :
所以这是我活动中的 OnCreate 方法:
public class MainActivity extends Activity {
private UiLifecycleHelper fbUiLifecycleHelper;
private ImageView playButton;
private ImageView friendsButton;
private TextView textRequest;
// Parameters of a WebDialog that should be displayed
private WebDialog dialog = null;
private String dialogAction = null;
private Bundle dialogParams = null;
LayoutInflater inflater;
private boolean gameLaunchedFromDeepLinking = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LayoutInflater factory = getLayoutInflater();
final View v = factory.inflate(R.layout.activity_main, null);
textRequest = (TextView)v.findViewById(R.id.textRequest);
playButton = (ImageView)v.findViewById(R.id.playButton);
playButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
onPlayButtonTouched();
return false;
}
});
friendsButton = (ImageView)v.findViewById(R.id.friendsButton);
friendsButton.setVisibility(View.INVISIBLE);
friendsButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
onFriendsButtonTouched();
return false;
}
});