why does only my xml layout work and have priority over the code inside OnCreate ? I am trying to make an imageview visible/invisible, but only the layout can make it, would you know why?

It is just a linearLayout vertical with :

<ImageView
        android:id="@+id/friendsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:src="@drawable/friends_button" />

The whole 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>

and friendsButton.setVisibility(View.INVISIBLE); in OnCreate

I tried to put it invisible in the layout and Visible in the code, but still the same priority to the layout...

Any idea?

Thanks


EDIT :


So this is the OnCreate method in my activity :

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;
            }
        });
有帮助吗?

解决方案

The behavior you are seeing is because of the way your onCreate() is set up:

setContentView(R.layout.activity_main);

final LayoutInflater factory = getLayoutInflater();
final View v = factory.inflate(R.layout.activity_main, null);

When setContentView(int) is called, R.layout.activity_main is inflated and your activity's view is set.

Following this, you are inflating R.layout.activity_main again:

final LayoutInflater factory = getLayoutInflater();
final View v = factory.inflate(R.layout.activity_main, null);

View v is not the same view as the one already set by setContentView(R.layout.activity_main, null). Now, using v, you initialize components:

textRequest = (TextView)v.findViewById(R.id.textRequest);

playButton = (ImageView)v.findViewById(R.id.playButton);

friendsButton = (ImageView)v.findViewById(R.id.friendsButton);

Again, these widgets do not correspond to the widgets you see in your activity. So, when you do the following:

friendsButton.setVisibility(View.INVISIBLE);

it does nothing to the activity view.

I hope you understand the problem here. Now, to the solution. There are two possible ways to fix this:

  • Find widgets in the view set by setContentView(R.layout.activity_main):

Remove:

final LayoutInflater factory = getLayoutInflater();
final View v = factory.inflate(R.layout.activity_main, null);

Use:

setContentView(R.layout.activity_main);

// Notice, we're not using v.findViewById(....)
textRequest = (TextView) findViewById(R.id.textRequest);
playButton = (ImageView) findViewById(R.id.playButton);

....
....    

friendsButton = (ImageView) findViewById(R.id.friendsButton);
friendsButton.setVisibility(View.INVISIBLE); // Will work now
  • Defer the call to setContentView(R.layout.activity_main) until the view has been inflated:

Keep your code as it is, but remove setContentView(R.layout.activity_main); from the beginning. Place it after setting up all the widgets.

....
....
friendsButton.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        onFriendsButtonTouched();
        return false;
    }
});

// Use the inflated View `v`
setContentView(v);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top