【发布时间】: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