2.7-andridstudio安装 建立项目 学习三种ui布局
上午通过教程安装完android studio
下午学习了相关知识 学习了三种ui布局
一:使用XML布局UI界面。
<LinearLayout 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"
android:orientation="horizontal" >
<EditText android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
二:写Java代码布局UI界面
setContentView(R.layout.activity_main);
FrameLayout frameLayout=new FrameLayout(this);//创建帧布局管理器
frameLayout.setBackground(this.getResources().getDrawable(R.drawable.ic_launcher));//设置背景
setContentView(frameLayout);//设置在Activity中显示的frameLayout
TextView text1=new TextView(this);
text1.setText("在JAVA代码中控制UI界面");//设置显示文字
text1.setTextSize(TypedValue.COMPLEX_UNIT_PX,20);//设置文字大小,单位为像素
text1.setTextColor(Color.rgb(100, 100, 100));//设置文字的颜色
frameLayout.addView(text1);//将text1添加到布局管理器中
TextView text2=new TextView(this);
text2.setText("程序正在载入......");//设置显示文字
text2.setTextSize(TypedValue.COMPLEX_UNIT_PX,20);//设置文字大小,单位为像素
text2.setTextColor(Color.rgb(200, 200, 100));//设置文字的颜色
LayoutParams params=new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);//创建保存布局参数的对象
params.gravity=Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL;//设置剧中显示
text2.setLayoutParams(params);//设置布局参数
frameLayout.addView(text2);
三:使用XML和Java代码混合布局UI界面。
XML实现方便快捷,但是缺少灵活性,Java实现灵活,但是开发过程繁琐。可以使用XML和Java混合使用来布局界面。把变化小,行为比较固定的组件放在XML里,把变化较多,行为控制比较复杂的组件来使用Java代码实现。
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
</LinearLayout>
public class MainActivity extends Activity {
private ImageView iView[] = new ImageView[2];
private int ImageID[] = new int[] { R.drawable.img01, R.drawable.img02 };
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) findViewById(R.id.ll);
LayoutParams params = new LayoutParams(255, 148);
for (int i = 0; i < ImageID.length; i++) {
iView[i] = new ImageView(this);
iView[i].setImageResource(ImageID[i]);
iView[i].setPadding(5, 5, 4, 5);
iView[i].setLayoutParams(params);
layout.addView(iView[i]);
}
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
浙公网安备 33010602011771号