安卓开发 从入门到转业 (一)

一、基本概念

开发商

Android是Google开发的操作系统。

开发工具

Eclipse(已废弃)

Android Studio

二、开始开发

AS连接mumu模拟器

首先下载一个mumu模拟器

进入bin目录下,打开cmd命令窗口,输入指令:

adb_server.exe connect 127.0.0.1:7555

在这里插入图片描述

AS已经可以看到mumu了:

在这里插入图片描述
成功在模拟器上运行:
在这里插入图片描述

第一个小程序

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

只有一个onCreate方法

布局管理器

相对布局(RelativeLayout)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="vertical"
    tools:context=".MainActivity">

    <RelativeLayout
        android:id="@+id/id01"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#000000" />

    <RelativeLayout
        android:id="@+id/id02"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_toRightOf="@id/id01"
        android:background="#ff0033"></RelativeLayout>

    <RelativeLayout
        android:id="@+id/id03"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@id/id02"
        android:background="#0066ff"
        android:padding="50dp">

        <LinearLayout
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:background="#ff0033"

            />
    </RelativeLayout>

</RelativeLayout>

与线性布局相比,多了一类标签。比如:android:layout_below=“@id/id02”

代表该标签的组件在目标组件的下面

线性布局(LinearLayout)

一个线性布局的demo

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#000000"
        android:orientation="horizontal"
        android:paddingLeft="20dp"
        android:paddingTop="20dp"
        android:paddingRight="20dp"
        android:paddingBottom="20dp">

        <view
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF0033"></view>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="10dp"
        android:background="#0066FF"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingLeft="20dp"
        android:paddingTop="20dp"
        android:paddingRight="20dp"
        android:paddingBottom="20dp">

        <view
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:background="#ffffff"
            android:orientation="horizontal"
            android:layout_weight="1"></view>
        <view
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@color/cardview_dark_background"
            android:layout_weight="1"></view>

    </LinearLayout>


</LinearLayout>

标签元素的含义都比较 见名知意

特别说明的是 android:layout_weight=“1” 这个元素 此处赋值1,代表的是比例,在同一个LinearLayout标签下的view,比如两个view块的layout_weight都是1,则代表两个view块的面积是1:1 其实这个数字就是这个标签在父标签页面上所占的比例(与其他标签相比)

TextView

实现

第一个页面跳转到第二个页面

第二个页面有text文本+icon

MainActivity

public class MainActivity extends AppCompatActivity {
    private Button buttonText01;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 把布局文件里的btn与此处的button绑定
        buttonText01 = findViewById(R.id.btn_text01);

        buttonText01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到text_view
                Intent intent = new Intent(MainActivity.this, ViewActivity.class);
                startActivity(intent);
            }
        });
    }
}

buttonText01.setOnClickListener(new View.OnClickListener() {} 一个按钮事件

ViewActivity

自动初始化的

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_text01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:text="TextView"
        android:layout_marginTop="200dp">

    </Button>

</LinearLayout>

主要是一个按钮button

activity_view.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"
    tools:context=".ViewActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我爱吃炸酱面"
        android:textColor="#000000"
        android:textSize="30sp"
        android:layout_marginTop="30dp"
        android:drawableRight="@drawable/customer">

    </TextView>

    <TextView
        android:layout_width="400px"
        android:layout_height="wrap_content"
        android:text="我超级爱吃炸酱面"
        android:textColor="#000000"
        android:textSize="30sp"
        android:ellipsize="end"
        android:maxLines="1"
        android:layout_marginTop="20dp">

    </TextView>

</LinearLayout>

android:ellipsize=“end” 代表超过文本框边界的文字用…代替

android:drawableRight=“@drawable/customer” 从drawable文件夹中读取icon

android:textSize=“30sp” 字体大小

android:maxLines=“1” 最大行数

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值