【问题标题】:Other Method isn't called onCreate其他方法未调用 onCreate
【发布时间】:2014-03-04 10:10:27
【问题描述】:

我们正在开发一个 Android 应用程序,我们希望在显示视图时重新布局我们的 UI。 由于我们无法计算 XML 文件中按钮的高度,因此我们想要计算它并显示它并抛出我们的 Java 代码。 但是我们的方法 fit() 在从 onCreate 调用时不起作用,而当我们从 Action 溢出调用它时它起作用。 我们在 Android 模拟器和三星 Galaxy SIII 上尝试了此代码。 有人可以帮助我们吗? 谢谢 Fotoschnitzel

代码如下:

public class Game extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.classic);

            fit();

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.settings:

            fit();

            default:
                return super.onOptionsItemSelected(item);
        }

    }


    public void fit() {

        Button Button1=(Button)findViewById(R.id.grid1);
        Button Button2=(Button)findViewById(R.id.grid2);
        Button Button3=(Button)findViewById(R.id.grid3);
        Button Button4=(Button)findViewById(R.id.grid4);
        Button Button5=(Button)findViewById(R.id.grid5);
        Button Button6=(Button)findViewById(R.id.grid6);
        Button Button7=(Button)findViewById(R.id.grid7);
        Button Button8=(Button)findViewById(R.id.grid8);
        Button Button9=(Button)findViewById(R.id.grid9);
        LinearLayout Lin1=(LinearLayout)findViewById(R.id.lin1);
        LinearLayout Lin2=(LinearLayout)findViewById(R.id.lin2);
        LinearLayout Lin3=(LinearLayout)findViewById(R.id.lin3);
        LinearLayout Global=(LinearLayout)findViewById(R.id.LinearLayout1);
        LinearLayout Up=(LinearLayout)findViewById(R.id.bestfiller);
        ProgressBar Bar=(ProgressBar)findViewById(R.id.progressBar1);

        int x = Button1.getWidth();
        int y = Global.getHeight();
        int z = Bar.getHeight();

        int Filler=y-3*x-z;
        if (Filler > 50){
        Up.setLayoutParams(new LinearLayout.LayoutParams(3*x, Filler));
        Lin1.setLayoutParams(new LinearLayout.LayoutParams(3*x, x));
        Lin2.setLayoutParams(new LinearLayout.LayoutParams(3*x, x));
        Lin3.setLayoutParams(new LinearLayout.LayoutParams(3*x, x));} 
        double size= x*0.4;
        Button1.setTextSize((float)size);
        Button2.setTextSize((float)size);
        Button3.setTextSize((float)size);
        Button4.setTextSize((float)size);
        Button5.setTextSize((float)size);
        Button6.setTextSize((float)size);
        Button7.setTextSize((float)size);
        Button8.setTextSize((float)size);
        Button9.setTextSize((float)size);

    }

}

【问题讨论】:

标签: java android xml layout oncreate


【解决方案1】:

我可以向您保证,在应用程序启动时也会调用该方法。你的问题是另一个问题。您正在检查 Filler 条件,但在 setContentView 之后

    int x = Button1.getWidth();
    int y = Global.getHeight();
    int z = Bar.getHeight();

xyz 为 0,使 if 条件为 false

【讨论】:

    【解决方案2】:

    视图层次结构尚未测量和布局,因此 getWidth()getHeight() 将返回 0。

    您可以将 Runnable 发布到 UI 线程消息队列,该队列在测量/布局传递后调用您的 fit() 方法。见How to retrieve the dimensions of a view?

    【讨论】:

      【解决方案3】:

      使用OnGlobalLayoutListener

      rootView.getViewTreeObserver().addOnGlovalLayoutListener( new OnGlobalLayoutListener() {
      
          public void onGlobalLayout() {
              //all views are measured now and getWidth() and getHeight() will return correct values
      
              //don't forget to unregister the listener for future layout events
              rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
      
          }
      
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-26
        • 1970-01-01
        • 1970-01-01
        • 2012-01-20
        • 1970-01-01
        相关资源
        最近更新 更多