Android之项目初体验

1.1 默认activity布局

(默认定义了两个组件:ConstraintLayout和TextView)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

组件是组成用户界面的构造模块,每一个组件是View类或其子类(如TextView或Button)的一个具体实例

1.2 自定义组件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="test"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="true" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="false"/>
    </LinearLayout>
    
</LinearLayout>

效果如下图
在这里插入图片描述

1.3 组件属性

1.3.1 android:layout_width和android:layout_height属性

几乎每个组件都需要这两个属性,通常被设置为以下两个属性值
match_parent:视图与其父视图大小相同
wrap_content:视图讲根据其内容自动调整大小

1.3.2 android:orientation属性

该属性决定子组件是水平放置还是垂直放置
vertical:垂直
horizontal:水平

1.3.3 android:text属性

TextView和Button组件都有该属性,该属性指定组件显示的内容
注意 该属性值不是字符串字面值,而是对字符串资源(string resources)的引用

1.4 创建字符串资源

每个项目都包含一个名为string.xml的默认字符串文件
在这里插入图片描述
添加字符串资源

<resources>
    <string name="app_name">My Application</string>
    <string name="question_text">Constantinople is the largest city in Turkey.</string>
    <string name="true_button">true</string>
    <string name="false_button">false</string>
</resources>

1.5 资源与资源ID

布局是一种资源,资源是应用非代码形式的内容。
项目的所有资源文件都存放在目录res的子目录下,通过包浏览器可以看到,布局activity_main.xml资源文件存放在res/layout目录下,包含字符串资源的string文件存放在res/values目录下。
可使用资源ID在代码中获取相应的资源。activity_main.xml文件定义的布局资源ID为R.layout.activity_main

1.5.1 为组件生成资源ID

要为组件生成资源ID,需在定义组件时为其添加上android:id属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="@string/question_text"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button" />

        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button"/>

    </LinearLayout>


</LinearLayout>

来源:《Android编程权威指南》
作者:Bill Phillips ,Brian Hardy
译:王明发
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值