0

请看下面的代码

游戏活动.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=".Game" >

    //Please note the GUI Has been removed


        <include layout="@layout/common_status_bar"/>


</RelativeLayout>

游戏.java

package game.games;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;

public class Game extends Activity {


    private ImageView goToLanguageSelection;
    private ImageView goToInternet;
    private Button giveUp;


    private LayoutInflater layoutInflater;
    private View commonView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        layoutInflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        commonView = layoutInflater.inflate(R.layout.common_status_bar, null);

        //Intializing instance variables
        goToLanguageSelection = (ImageView)commonView.findViewById(R.id.backToLanguageSelectionButton);
        goToInternet = (ImageView)commonView.findViewById(R.id.internetButton);
        giveUp = (Button)commonView.findViewById(R.id.giveUpButton);

        flag = getIntent().getBooleanExtra("LANGUAGE", true);

        //Add listeners

        goToLanguageSelection.setOnClickListener(goToLanguageSelectionClicked);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.game, menu);
        return true;
    }








    //Will get activated when the ThunderBolt image is clicked  
    private OnClickListener goToLanguageSelectionClicked = new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            Intent intent = new Intent(getBaseContext(),LanguageSelector.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);

        }

    };


}

common_status_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#373734"
    android:orientation="horizontal" >



        <ImageView
            android:id="@+id/backToLanguageSelectionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="15dp"
            android:paddingTop="5dp"
            android:src="@drawable/thunderbolt" />

        <ImageView
            android:id="@+id/internetButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:src="@drawable/globe_small_2" />

        <ImageView
            android:id="@+id/justForFun"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="5dp"
            android:paddingTop="5dp" />

         <Button
            android:id="@+id/giveUpButton"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:text="Button" />


</LinearLayout>

在这里,common_status_bar.xml是一个常见的布局。我已将此布局添加到game_activity.xmlby <include layout="@layout/common_status_bar"/>

它显示一切正常,没有问题。但情况是我不能让它的任何元素起作用。我已将 a 附加OnClickListener到它的第一个元素,goToLanguageSelection但它没有响应单击。我通过将点击事件添加到所有其他按钮、图像(一次一个)来测试这一点,我得到了相同的结果。它没有响应用户点击或其他任何内容。

为什么这个按钮和单独布局的图像不响应用户事件?

4

1 回答 1

1

commonView不应再次创建。

如果您想保留 的引用common_status_bar.xml,可以通过 id 为其命名,如下面的代码:

    <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="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp" >

        <include
            android:id="@+id/common_status_bar"
            layout="@layout/common_status_bar" />

    </RelativeLayout>

然后,更改此代码:

       commonView = layoutInflater.inflate(R.layout.common_status_bar, null);

至:

       commonView = findViewById(R.id.common_status_bar);
于 2013-07-19T10:04:45.880 回答