Android学习记录(二)

Android学习目录(二)

在进行第一次Andriod的学习后,我了解到了Android的基本布局方式,并编写了两个案例。

1.复习内容

为了能够更好的进行学习内容的巩固,在进行今天的学习内容之前,首先我会进行基础的线性布局的相关复习。并通过编写案例来进行展示。

1.1 复习案例展示

在今天的学习内容中,我选择复习Android中最为常见的线性布局。所做案例的相关图示如下图所示:
在这里插入图片描述

1.2案例思路:

进行案例编写之前,我们首先需要进行思考,为了能够更加简便的做出自己想要的效果。首先,我将整个页面分为三个部分,在整个页面中最重要的部分便是页面之间的五个块。在该部分,我使用了线性布局,并将每个框都会使用到两个文本框进行框选,并通过设置边距来进行位置的调整。

1.3相关代码

在编写该项程序时,我认为最能体现结构的部分就是activity_main.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:background="@drawable/background"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="360dp"
        android:layout_height="30dp"
        android:layout_marginTop="155dp"
        android:layout_gravity="center"
        android:alpha="0.8"
        android:background="@drawable/shape_round_coener"
        android:gravity="center">
        <TextView
            android:layout_width="180dp"
            android:layout_height="30dp"
            android:background="@drawable/shape_round_coener"
            android:textColor="#000066"
            android:text="@string/part01"
            android:layout_marginLeft="1dp"
            android:gravity="center"
            android:layout_gravity="left"/>

        <TextView
            android:layout_width="180dp"
            android:layout_height="30dp"
            android:background="@drawable/shape_round_coener01"
            android:textColor="@color/white"
            android:gravity="center"
            android:text="@string/part02"
            android:layout_gravity="center"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="12dp"
        android:layout_height="10dp"
        android:background="@drawable/logo"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="300dp"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@drawable/list_bg">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/list_logo"
            android:layout_marginRight="-100dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@drawable/list_bg2">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/list_logo2"
            android:layout_marginRight="-100dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@drawable/list_bg3">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/list_logo3"
            android:layout_marginRight="-100dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@drawable/list_bg4">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/list_logo4"
            android:layout_marginRight="-100dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@drawable/list_bg5">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/list_logo5"
            android:layout_marginRight="-100dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="405dp"
        android:layout_height="170dp"
        android:background="@color/white"
        android:gravity="top"
        >
        <LinearLayout
            android:layout_width="135dp"
            android:layout_height="20dp"
            android:gravity="center"
            >
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/logo1"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="首页"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="135dp"
            android:layout_height="20dp"
            android:gravity="center"
            >
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/logo2"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="消息"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="135dp"
            android:layout_height="20dp"
            android:gravity="center"
            >
            <ImageView
                android:layout_width="21dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:src="@drawable/logo3"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="个人中心"/>
        </LinearLayout>
    </LinearLayout>


</LinearLayout>

2.学习内容

2.1下拉列表的实现

再进行复习内容后,我打算进行一些新内容的学习,在以前学习网页设计时,我常使用的东西就是下拉列表,所以便想在Android中也尝试一下,看是否能够实现。最后效果,如图所示:
在这里插入图片描述
相关代码如图所示:

<!--下拉列表01 -->
<LinearLayout
    android:layout_width="380dp"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:padding="15dp"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvTestSubject"
        android:text="@string/test_subject"
        android:textColor="@color/black"/>

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/spTestSubject"
        android:entries="@array/subjects"/>
</LinearLayout>

在代码中所涉及到的string文件中的内容可根据自己的喜好进行编写,在这就不进行详细展示。

2.2单选按钮的实现

实现效果如下:
在这里插入图片描述
相关代码如下所示:
1.activity_main.xml文件

<!--单选按钮-->
    <LinearLayout
        android:layout_width="380dp"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvGender"
            android:textColor="@color/black"
            android:text="@string/gender1"/>
        <RadioGroup
            android:layout_width="340dp"
            android:layout_height="wrap_content"
            android:id="@+id/rgGender"
            android:orientation="horizontal">
            <RadioButton
                android:layout_width="160dp"
                android:layout_height="wrap_content"
                android:id="@+id/rbMale"
                android:checked="true"
                android:text="@string/gender1_1"/>
            <RadioButton
                android:layout_width="160dp"
                android:layout_height="wrap_content"
                android:id="@+id/rbFemale"
                android:layout_marginLeft="15dp"
                android:text="@string/gender1_2"/>
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="360dp"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvGender02"
            android:textColor="@color/black"
            android:text="@string/gender2"/>
        <RadioGroup
            android:layout_width="340dp"
            android:layout_height="wrap_content"
            android:id="@+id/rgGender02"
            android:orientation="horizontal">
            <RadioButton
                android:layout_width="160dp"
                android:layout_height="wrap_content"
                android:id="@+id/rbMale02"
                android:checked="true"
                android:text="@string/gender2_1"/>
            <RadioButton
                android:layout_width="160dp"
                android:layout_height="wrap_content"
                android:id="@+id/rbFemale02"
                android:layout_marginLeft="15dp"
                android:text="@string/gender2_2"/>
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="360dp"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvGender03"
            android:textColor="@color/black"
            android:text="@string/gender1"/>
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rgGender03"
            android:orientation="horizontal">
            <RadioButton
                android:layout_width="160dp"
                android:layout_height="wrap_content"
                android:id="@+id/rbMale03"
                android:checked="true"
                android:text="@string/gender3_1"/>
            <RadioButton
                android:layout_width="160dp"
                android:layout_height="wrap_content"
                android:id="@+id/rbFemale03"
                android:layout_marginLeft="15dp"
                android:text="@string/gender3_2"/>
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="360dp"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvGender04"
            android:textColor="@color/black"
            android:text="@string/gender4"/>
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rgGender04"
            android:orientation="horizontal">
            <RadioButton
                android:layout_width="160dp"
                android:layout_height="wrap_content"
                android:id="@+id/rbMale04"
                android:checked="true"
                android:text="@string/gender4_1"/>
            <RadioButton
                android:layout_width="160dp"
                android:layout_height="wrap_content"
                android:id="@+id/rbFemale04"
                android:layout_marginLeft="15dp"
                android:text="@string/gender4_2"/>
        </RadioGroup>
    </LinearLayout>

2.strings.xml文件

<string name="gender1">变速箱:</string>
    <string name="gender1_1">自动</string>
    <string name="gender1_2">手动</string>
    <string name="gender2">轮毂:</string>
    <string name="gender2_1">烤漆</string>
    <string name="gender2_2">电镀</string>
    <string name="gender3">中控:</string>
    <string name="gender3_1">低配</string>
    <string name="gender3_2">高配</string>
    <string name="gender4">刹车:</string>
    <string name="gender4_1">鼓式制动机</string>
    <string name="gender4_2">盘式制动机</string>

2.3输入框的实现

具体效果如下所示:
在这里插入图片描述
相关代码如下所示:
1.activity_main.xml文件

<!--输入框-->
    <LinearLayout
        android:layout_width="380dp"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:paddingTop="20dp"
        android:paddingLeft="20dp"
        android:paddingRight="30dp">
        <LinearLayout
            android:layout_width="380dp"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tvName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/name"
                android:textColor="@color/black" />

            <EditText
                android:id="@+id/edtName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/input_name"
                android:singleLine="true" />
        </LinearLayout>
    </LinearLayout>

相关的strings.xml文件编写与平常的基本编写方法相同,在这里便省略。

最后实现效果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46705517/article/details/112367604