0

为什么只有我的 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;
            }
        });
4

1 回答 1

1

您看到的行为是因为您onCreate()的设置方式:

setContentView(R.layout.activity_main);

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

setContentView(int)被调用时,R.layout.activity_main被膨胀并且你的活动的视图被设置。

在此之后,您R.layout.activity_main 再次膨胀:

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

视图v与已设置的视图不同setContentView(R.layout.activity_main, null)。现在,使用v初始化组件:

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

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

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

同样,这些小部件与您在活动中看到的小部件不对应。因此,当您执行以下操作时:

friendsButton.setVisibility(View.INVISIBLE);

它对活动视图没有任何作用。

我希望你能理解这里的问题。现在,解决方案。有两种可能的方法来解决这个问题:

  • 在由以下设置的视图中查找小部件setContentView(R.layout.activity_main)

消除:

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

利用:

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
  • 推迟调用setContentView(R.layout.activity_main)直到视图膨胀:

保持您的代码原样,但从setContentView(R.layout.activity_main);一开始就删除。在设置所有小部件后放置它。

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

// Use the inflated View `v`
setContentView(v);
于 2013-08-14T23:41:19.550 回答