View类与View的子类
View类
android:id属性
android:id = "@+id/user"
android:background属性
android:background="@mipmap/bg"//图片资源,需要先将bg保存到mipmap中
android:background="#FFFFFF"//设置颜色
android:padding属性
android:padding="16dp"//设置内边距
android:padding="@dimen/activity_margin"//另一种设置方法
对内边距进行修改:
ViewGroup:用来控制view是如何安放的
ViewGroup.layoutparams:
android:layout_height属性和android:layout_width属性
ViewGroup.MarginLayoutParams:
控制UI界面
1.使用XML布局文件控制UI界面(推荐)
在android应用的res/layout目录下编写xml文件(res->layout->activity_main.xml)
在Activity中使用java代码显示
setContentView(R.layout.activity_main)
所有的UI界面都是在布局.xml中配置的
代码示例:
<?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:background="@mipmap/bg" //添加背景图片
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" //将文本框设置成居中
android:textSize="18sp" //设置文本框的大小
android:textColor="#115572" //设置文本框的颜色
android:text="@string/start"/> //设置文本框的内容
</FrameLayout>
2.在java代码中控制UI界面
1.启动布局管理(添加一些设置,例如北京,文字框,颜色,布局参数)
2.添加布局的逻辑代码,事件
3.将类添加到布局管理器中
将MainActivity中的
setContentView(R.layout.activity_main);
删除,并在下面添加布局相关代码。
public class MainActivity extends AppCompatActivity {
@Override
protected void