ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android开发(第一天)

2021-12-23 22:37:26  阅读:279  来源: 互联网

标签:layout 第一天 开发 activity Android onCreate TextView view


开发工具的安装

选择使用Android Studio进行开发,Android Studio 是谷歌推出的一个Android集成开发工具,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提供了集成的 Android 开发工具用于开发和调试。
在此附上大佬的安装教程:Android Studio安装教程

UI组件

1 布局管理器

  • 线性布局(LinearLayout)
  • 相对布局(RelativeLayout)

1.1 LinearLayout

常用属性:

  • id
  • layout_width
    • match_parent 匹配父控件
    • wrap_content 填充文字
  • layout_height
  • background
  • layout_margin
  • layout_padding
  • orientation
<?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:id="@+id/ll_1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#8BC34A"
        android:orientation="vertical"
        android:padding="20dp">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#565555"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#8B634A"
        android:orientation="horizontal"
        android:layout_marginTop="20dp"
        android:gravity="bottom">

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#FFC107"
            android:layout_weight="1"/>
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

    </LinearLayout> 

</LinearLayout>

image

1.2 RelativeLayout

layout_

  • toLeftOf
  • toRightOf
  • alignBottom
  • alignParentBottom
  • below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/view_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#4600108D" />

    <View
        android:id="@+id/view_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#FF5722"
        android:layout_toRightOf="@id/view_1"/>

    <View
        android:id="@+id/view_3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#46BCC719"
        android:layout_below="@id/view_2"/>

    <LinearLayout
        android:id="@+id/ll_1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@id/view_3"
        android:background="#888888"
        android:orientation="horizontal"
        android:padding="15dp">

        <View
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="#465456"/>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000000"
            android:padding="15dp">

            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#888888"/>

        </RelativeLayout>

    </LinearLayout>

</RelativeLayout>

image

TextView控件

创建按钮新建一个空白活动

在activity_main.xml中创建一个按钮用于点击跳转

<Button
        android:id="@+id/bt_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮"/>

在MainActivity.java中添加bt_1的点击事件使其跳转到TextViewActivity

public class MainActivity extends AppCompatActivity {

    private Button mbtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mbtn = findViewById(R.id.bt_1);
        mbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到演示界面
                Intent intent = new Intent(MainActivity.this,TextViewActivity.class);
                startActivity(intent);
            }
        });
    }
}

创建一个新的TextViewActivity,在activity_text_view.xml中创建TextView控件并实现以下功能

  • 文字在一行中显示并标注未现实完全

activity_text_view.xml

<TextView
        android:id="@+id/tv_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="学习Android的第一天"
        android:textSize="50sp"
        android:layout_marginTop="10dp"/>

效果显示图:
image

  • 实现文字和图片在一个TextView中

activity_text_view.xml

<TextView
        android:id="@+id/tv_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:text="树莓派"
        android:drawableRight="@drawable/ic_launcher_background"
        android:textSize="50sp"/>

效果显示图:
image

  • 实现文字删除线

创建一个普通TextView

<TextView
        android:id="@+id/tv_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hzy"
        android:textSize="50sp"/>

在java中进行添加删除线的效果

public class TextViewActivity extends AppCompatActivity {
    private TextView mtv4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);
        mtv4 = findViewById(R.id.tv_4);
        mtv4.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//中划线
        mtv4.getPaint().setAntiAlias(true);//去除锯齿
    }
}

效果显示图
image

  • 实现下划线效果

如上,创建组件

<TextView
        android:id="@+id/tv_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hzy"
        android:textSize="24sp"/>

在java代码中做修改

public class TextViewActivity extends AppCompatActivity {
    private TextView mtv5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);
        mtv5 = findViewById(R.id.tv_1);
        mtv5.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
    }
}

效果显示:
image

  • 实现文字走马灯效果

.xml文件

<TextView
        android:id="@+id/tv_7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dididiididahhdesfsdfdsfsdfsdfsdfsdfdsf"
        android:textSize="34sp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_marginTop="10dp"/>

显示效果图:
image

注意:遇事不决,清理缓存并重启

标签:layout,第一天,开发,activity,Android,onCreate,TextView,view
来源: https://www.cnblogs.com/java-six/p/15725465.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有