实验二 Android UI布局以及简单组建的学习
一、实验目的
1.学习Absolute、LinearLayout、FramLayout、RelativeLayout
2.掌握layout_weight、lauout_gravity、gravity的用法
3.熟练使用TextView EditText Button等组建的使用
4.理解并实现简单的布局之间的嵌套
5.实现简单的QQ登录的界面,并在QQ登录界面添加一个文本框实现百度超链接的跳转。
6.至少实现一种按钮的点击事件
二、实验主要仪器设备
Window 7 系统
JDK、Android Studio
三、实验内容及要求
1. 独立完成所给截图的代码设计以及最后运行成功的结果
2.在做实验的过程中尽量多的将能用到的属性都用上
3.所有生成的APP请以各自的功能命名+自己学号的后三位
四、实验内容图片
四、代码
第一个图片:
下面展示一些 内联代码片
。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A5A2A2"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/RelativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:layout_marginLeft="70dp"
android:text="用户"
android:textSize="45dp">
</TextView>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFEFE"
android:layout_marginTop="60dp"
android:layout_marginLeft="200dp"
android:layout_marginRight="45dp"
android:textSize="60dp">
</Button>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/RelativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:layout_marginLeft="70dp"
android:text="密码"
android:textSize="45dp">
</TextView>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFE"
android:layout_marginTop="60dp"
android:layout_marginLeft="200dp"
android:layout_marginRight="45dp"
android:textSize="60dp">
</Button>
</RelativeLayout>
</RelativeLayout>
第二个图片:
下面展示一些 内联代码片
。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="fill_parent"
android:layout_height="180dp"
android:text="上">
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="40dp"
android:layout_height="220dp"
android:text="左">
</Button>
<Button
android:layout_width="330dp"
android:layout_height="220dp"
android:text="中">
</Button>
<Button
android:layout_width="fill_parent"
android:layout_height="220dp"
android:text="右">
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="下">
</Button>
</LinearLayout>
</LinearLayout>
第三个图片:
下面展示一些 内联代码片
。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center"
android:background="#F3050B"
android:visibility="visible">
</Button>
<Button
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:background="#D5C327"
android:visibility="visible">
</Button>
<Button
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:background="#4ACC1B"
android:visibility="visible">
</Button>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#11D1BB"
android:visibility="visible">
</Button>
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#0B3EF1"
android:visibility="visible">
</Button>
</FrameLayout>
第四个图片:
下面展示一些 内联代码片
。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="100dp"
android:layout_height="80dp"
android:id="@+id/button1"
android:layout_centerInParent="true"
android:text="中"
android:textSize="27dp"
android:textColor="#F30606">
</Button>
<Button
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_below="@id/button1"
android:layout_alignLeft="@id/button1"
android:text="下"
android:textSize="27dp"
android:textColor="#30E611">
</Button>
<Button
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_above="@id/button1"
android:layout_alignLeft="@id/button1"
android:text="上"
android:textSize="27dp"
android:textColor="#0A0A0A">
</Button>
<Button
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/button1"
android:text="左"
android:textSize="27dp"
android:textColor="#FFFEFE">
</Button>
<Button
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/button1"
android:text="右"
android:textSize="27dp"
android:textColor="#6666E0">
</Button>
</RelativeLayout>
第五个图片:
下面展示一些 内联代码片
。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#E91E63">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#2DD51A">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#1B7DBE">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FFEB3B">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#9C27B0">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#070707">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ED8DFA">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#74E9F8">
</TextView>
</LinearLayout>
</LinearLayout>
第六个图片:
下面展示一些 内联代码片
。
Activitymain_Xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#C1C1C2"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp">
<ImageView
android:layout_width="120dp"
android:layout_height="130dp"
android:layout_marginTop="100dp"
android:layout_weight="1"
android:src="@mipmap/head">
</ImageView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textSize="40dp"
android:background="#FAFAFC"
android:layout_marginLeft="10dp">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FAFAFC"
android:layout_marginRight="10dp">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="40dp"
android:background="#FAFAFC"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FAFAFC"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:inputType="textPassword">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:background="#437EF1"
android:text="登录"
android:textSize="41dp"
android:layout_marginTop="50dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp">
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="百度求解http://www.baidu.com"
android:textSize="25dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="30dp"
android:background="#E91E63"
android:autoLink="web">
</TextView>
</LinearLayout>
</LinearLayout>
MainActivity_java代码:
package com.example.shiyaner6;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
//定义对象
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定控件
btn=findViewById(R.id.button1);
//按钮单击事件
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//按钮被单击后的操作
//改变按钮上的文字
btn.setText("很开心遇到你!");
btn.setBackgroundColor(Color.rgb(0,30,40));
}
});
}
}