4

昨日アプリをGoogle Playにアップロードしましたが、今朝、テキストの一部が小さな画面のボタンに重なっていたため、レイアウトを微調整したかったのですが、基本的にはボタンを画面の下に移動したいだけです. これは、Eclipse のグラフィカル エディターを使用するのと同じくらい簡単だと思いました...いいえ。

理由はわかりませんが、「view_fact」レイアウトのボタンの位置に対して行った小さな編集により、ボタンが間違った OnClick リスナーに登録されました。ビューには 2 つのボタンしかなく、お互いのイベント リスナーを使用しています。理由がわかりません。古いレイアウトで完全に機能していたイベント リスナー コードには触れませんでした。

これが私のview_factレイアウトです:

<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" >

<TextView
    android:id="@+id/viewFactTitleText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="18dp"
    android:text="@string/factTitleText"
    android:textSize="22dp"
    tools:context=".MainActivity" />

<ImageView
    android:id="@+id/randomFactImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/viewFactTitleText"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="18dp"
    android:contentDescription="Fact Image"
    android:src="@drawable/canadaflag" />

<TextView
    android:id="@+id/factData"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/randomFactImage"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:text="TextView" />

<Button
    android:id="@+id/anotherFactButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/backToHomeButton"
    android:layout_alignLeft="@+id/backToHomeButton"
    android:layout_alignRight="@+id/backToHomeButton"
    android:text="@string/anotherFactButtonText" />

<Button
    android:id="@+id/backToHomeButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/factData"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/factData"
    android:text="@string/backToHomeButtonText" />


</RelativeLayout>

リスナーと起動コード:

public class MainActivity extends Activity {
/* Declaration of global variables */
private boolean debugMode = true; // Whether debugging is enabled or not 

private static String logtag = "CanadianFacts"; // For use as the tag when logging 
private TextView factData;
private int totalFacts = 72;
private String[][] facts = new String[totalFacts][5];
private int lastFact = 0;


/* Buttons */
/* Home page */
private Button randomFactButton;

/* View Fact page */
private Button anotherRandomFactButton;
private Button backToHomeButton;

/* About page */
private Button backToHomeFromAboutButton;

/* Image Views */
private ImageView randomFactImage;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /* Home Page Objects */
    randomFactButton = (Button)findViewById(R.id.randomFactButton);
    randomFactButton.setOnClickListener(randomFactListener); // Register the onClick listener with the implementation above

    /* View Fact Page Objects */


    /* Build Up Fact Array */
    buildFactArray();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_about:
            loadAboutPage(); // Call the loadAboutPage method
            return true;
     }

    return false;
}

public void loadAboutPage() {
    setContentView(R.layout.about);

    /* Set up home page button listener */
    backToHomeFromAboutButton = (Button)findViewById(R.id.backToHomeFromAboutButton); 
    backToHomeFromAboutButton.setOnClickListener(backToHomeListener); // We can reuse the backToHomeListener

}

/* Home Page Listeners */
//Create an anonymous implementation of OnClickListener, this needs to be done for each button, a new listener is created with an onClick method
private OnClickListener randomFactListener = new OnClickListener() {
    public void onClick(View v) {
        if (debugMode) {
            Log.d(logtag,"onClick() called - randomFact button");              
            Toast.makeText(MainActivity.this, "The random fact button was clicked.", Toast.LENGTH_LONG).show();
        }

      setContentView(R.layout.view_fact); // Load the view fact page

      /* We're now on the View Fact page, so elements on the page are now in our scope, instantiate them */

      /* Another Random Fact Button */
      anotherRandomFactButton = (Button)findViewById(R.id.anotherFactButton);
      anotherRandomFactButton.setOnClickListener(anotherRandomFactListener); // Register the onClick listener with the implementation above

      /* Back to Home Button */
      backToHomeButton = (Button)findViewById(R.id.backToHomeButton);
      backToHomeButton.setOnClickListener(backToHomeListener); // Register the onClick listener with the implementation above

      // Get a random fact
      String[] fact = getRandomFact();

      if (fact[2] == null) { // If this fact doesn't have an image associated with it
          fact[2] = getRandomImage();
      }

      int imageID = getDrawable(MainActivity.this, fact[2]);

      /* See if this fact has an image available, if it doesn't select a random generic image */
      randomFactImage = (ImageView) findViewById(R.id.randomFactImage);
      randomFactImage.setImageResource(imageID);

      factData = (TextView) findViewById(R.id.factData);
      factData.setText(fact[1]);

        if (debugMode) {
            Log.d(logtag,"onClick() ended - randomFact button");
        }
      }
};


/* View Fact Page Listeners */
private OnClickListener anotherRandomFactListener = new OnClickListener() {
    public void onClick(View v) {
        if (debugMode) {
            Log.d(logtag,"onClick() called - anotherRandomFact button");              
            Toast.makeText(MainActivity.this, "The another random fact button was clicked.", Toast.LENGTH_LONG).show();
        }

          // Get a random fact
          String[] fact = getRandomFact();

          if (fact[2] == null) { // If this fact doesn't have an image associated with it
              fact[2] = getRandomImage();
          }

          int imageID = getDrawable(MainActivity.this, fact[2]); // Get the ID of the image

          /* See if this fact has an image available, if it doesn't select a random generic image */
          randomFactImage = (ImageView) findViewById(R.id.randomFactImage);
          randomFactImage.setImageResource(imageID);

          factData = (TextView) findViewById(R.id.factData);
          factData.setText(fact[1]);

          if (debugMode) {
              Log.d(logtag,"onClick() ended - anotherRandomFact button");
          }
    }
};

private OnClickListener backToHomeListener = new OnClickListener() {
    public void onClick(View v) {
        if (debugMode) {
            Log.d(logtag,"onClick() called - backToHome button");              
            Toast.makeText(MainActivity.this, "The back to home button was clicked.", Toast.LENGTH_LONG).show();
        }

          // Set content view back to the home page
          setContentView(R.layout.main); // Load the home page

          /* Reinstantiate home page buttons and listeners */
          randomFactButton = (Button)findViewById(R.id.randomFactButton);
          randomFactButton.setOnClickListener(randomFactListener); // Register the onClick listener with the implementation above

          if (debugMode) {
              Log.d(logtag,"onClick() ended - backToHome button");
          }
    }
};

ありがとうございました。

4

1 に答える 1

2

ボタンを移動し、IDを数回変更してから元に戻すことで、これを修正できました。すべての整列設定を削除し、その位置をリセットします。

おそらくEclipseのグラフィカルエディタが原因で、非常に奇妙な問題です。

于 2012-10-06T12:14:06.797 に答える