Android的UI书写的四种方法

View类与View的子类

mark

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"//另一种设置方法

对内边距进行修改:

mark

ViewGroup:用来控制view是如何安放的

ViewGroup.layoutparams:

android:layout_height属性和android:layout_width属性

ViewGroup.MarginLayoutParams:

mark

控制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.将类添加到布局管理器中

mark

将MainActivity中的

setContentView(R.layout.activity_main);

删除,并在下面添加布局相关代码。

  public class MainActivity extends AppCompatActivity {
   

    @Override
    protected void 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值