4

我昨天将我的应用程序上传到了 Google Play,今天早上我只想进行布局调整,因为一些文本在较小的屏幕上与按钮重叠,基本上我只想将按钮移到屏幕下方。我认为这就像使用 eclipse 的图形编辑器一样简单……不。

我不知道为什么,但是我对“view_fact”布局上按钮位置所做的小编辑已将按钮注册到错误的 OnClick 侦听器中,视图上只有两个按钮,它们正在使用 eachothers 事件侦听器我不知道为什么。我没有触及在旧布局上完美运行的事件侦听器代码。

这是我的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 回答