您的位置:首页 > 产品设计 > UI/UE

use java code to cheng the layout of the UI

2016-05-23 00:00 459 查看
摘要: this is an example about the change of UI at out phone only by using the java code

the code is that:

[code=language-java]public class Java extends Activity {
private TextView tv1, tv2, tv3, tv4;
private int screenWidth, screenHeight;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java);
initView();
WindowManager manager = getWindowManager();
DisplayMetrics metrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, (int)(screenHeight*0.1));
params.topMargin=5;
tv1.setLayoutParams(params);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, (int)(screenHeight*0.2));
params1.topMargin=5;
tv2.setLayoutParams(params1);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, (int)(screenHeight*0.3));
params2.topMargin=5;
tv3.setLayoutParams(params2);
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
params3.topMargin=5;
tv4.setLayoutParams(params3);
}

private void initView() {
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
tv4 = (TextView) findViewById(R.id.tv4);
}

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

2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息