美文网首页
Android APP学习(2)---- 基础demo 和工程文

Android APP学习(2)---- 基础demo 和工程文

作者: 特立独行的佩奇 | 来源:发表于2022-03-18 21:04 被阅读0次

Android 基础demo

MainActivity.java代码:

package com.example.activitytest;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

activity_main.xml 文件内容:

<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="63dp"
        tools:layout_editor_absoluteY="16dp">

        <Button
            android:id="@+id/button"
            android:layout_marginTop="200dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

代码中只是创建了一个empty activity,没有做任何事;activity 的布局里也只有一个button.
显示效果如下:


my_applicaiton.png

Android 工程目录解析

目录 说明
java java/com/example/activitytest/ 存放java代码的地方,业务功能都在这里实现
res 存放各种资源文件的地方,有图片,字符串,动画,音频等,还有各种形式的XML文件
assets 不一定有,也是存放资源文件的地方
AndroidManifest.xml android 项目的配置文件

res目录和assets 都是存放资源文件的地方,两者的区别在于是否前者下所有的资源文件都会在R.java文件下生成对应的资源id,而后者并不会;

前者我们可以直接通过资源id访问到对应的资源;而后者则需要我们通过AssetManager以二进制流的形式来读取

图片资源
元素 说明
drawable 存放各种位图文件,(.png,.jpg,.9png,.gif等)除此之外可能是一些其他的drawable类型的XML文件
mipmap-hdpi 高分辨率,一般我们把图片丢这里
mipmap-mdpi 中等分辨率,很少,除非兼容的的手机很旧
mipmap-xhdpi 超高分辨率,手机屏幕材质越来越好,以后估计会慢慢往这里过渡
mipmap-xxhdpi 超超高分辨率,这个在高端机上有所体现
布局资源
元素 说明
layout 该目录下存放布局文件,另外在一些特定的机型上,我们做屏幕适配,比如480*320这样的手机,我们会另外创建一套布局,就行:layout-480x320这样的文件夹

布局文件解析:


layout文件.png
<FrameLayout   
android:layout_width=“xxdp” //取值范围”match_parent”、”wrap_content”、”XXdp”  布局整体宽度  
android:layout_height=“”//取值范围”match_parent”、”wrap_content”、”XXdp”  布局整体高度  
android:layout_weight=“”//取值示例”1.0”  布局所占比重  
android:layout_gravity=“” //取值范围”center”、”top”、”bottom”、”left”、”right”、”right|bottom” 布局位置  
android:layout_margin=“” //取值示例”0dp” 距离父控件四个方向距离  
android:layout_marginLeft=“” //取值示例”0dp” 距离父控件左间距  
android:layout_marginTop=“” //取值示例”0dp” 距离父控件上间距  
android:layout_marginRight=“” //取值示例”0dp” 距离父控件右间距  
android:layout_marginBottom=“” //取值示例”0dp” 距离父控件下间距  
android:layout_marginStart=“” //取值示例”-50dp” “50dp”  控件离父控件开始的位置的距离(前者不显示前50dp)  
android:layout_marginEnd=“”   //取值示例”-50dp” “50dp”  控件离父控件结束的位置的距离(前者不显示后50dp)  
android:addStatesFromChildren=“” //取值范围”true” 、”false” viewgroup的drawable属性是否把它的子类的drawable的state包含进来。测试中linearlayout如果不包含该属性(false),当子widget被点击时不会出现被选中的状态。也就是子类的state不会被传递给父类了  
android:descendantFocusability=“” //见下  
android:splitMotionEvents=“”  //取值范围”true” “false” 定义布局是否传递touch事件到子布局  
android:layoutMode=“” //取值范围 “clipBounds” “opticalBounds”  见下  
android:id=“”   
android:tag=“” //listView中区别Item非常有用的Tag标签(key-value)  
android:scrollX=“” //The initial horizontal scroll offset, in pixels. [dimension]最初的水平滚动的偏移,以像素为单位。  
android:scrollY=“” //The initial virtual scroll offset, in pixels. [dimension]最初的竖直滚动的偏移,以像素为单位。  
android:background=“”   
android:padding=“” //取值示例”0dp” 控件内容(文本图片子控件等)距离控件四个方向边界的距离  
android:paddingLeft=“”   
android:paddingTop=“”   
android:paddingRight=“”   
android:paddingBottom=“”   
android:paddingStart=“” //见下  文字对齐方式详解  
android:paddingEnd=“” //见下  文字对齐方式详解  
android:foreground=“” //设置布局的前景图,前景图不会被子元素覆盖  
android:foregroundGravity=“” //设置布局前景图的位置  
android:foregroundInsidePadding=“” //  
android:focusable=“” //取值”true” “false”  能否获得焦点(按键)  
android:focusableInTouchMode=“” //取值”true” “false”  是否可以通过touch获取到焦点(android:focusable 为先决条件)  
android:fitsSystemWindows=“” //取值”true” “false”  设置布局调整时是否考虑系统窗口(如状态栏)  
android:fadeScrollbars=“” //取值”true” “false” 滚动条自动隐藏  
android:fadingEdge=“”  //设置拉滚动条时 ,边框渐变的方向。none(边框颜色不变),horizontal(水平方向颜色变淡),vertical(垂直方向颜色变淡)。  
android:fadingEdgeLength=“” //滚动条渐变长度  
android:filterTouchesWhenObscured=“” //取值范围  “true” “false”所在窗口被其它可见窗口遮住时,是否过滤触摸事件  
android:visibility=“” //取值范围 “gone” “visible” “invisible”(虽不可见,但占据布局位置)  
android:scrollbars=“”//取值范围 “none” “horizontal” “vertical” 设置滚动条  
android:scrollbarStyle=“” //outsideInset :  该ScrollBar显示在视图(view)的边缘,增加了view的padding. 如果可能的话,该ScrollBar仅仅覆盖这个view的背景.                                  insideInset :该ScrollBar显示在padding区域里面,增加了控件的padding区域,该ScrollBar不会和视图的内容重叠.                                                    outsideOverlay :  该ScrollBar显示在视图(view)的边缘,不增加view的padding,该ScrollBar将被半透明覆盖                                                         insideOverlay : 该ScrollBar显示在内容区域里面,不会增加了控件的padding区域,该ScrollBar以半透明的样式覆盖在视图(view)的内容                      
android:isScrollContainer=“”  //取值范围 “true”“false” 设置当前View是否为滚动容器(是否可以为输入法腾出空间而隐藏)  
android:scrollbarFadeDuration=“” //褪色时间  
android:scrollbarDefaultDelayBeforeFade=“” //设置滚动条N毫秒后开始淡化,以毫秒为单位。  
android:scrollbarSize=“” //设置滚动条大小  
android:scrollbarThumbHorizontal=“@drawable” //设置水平滚动条的drawable  
android:scrollbarThumbVertical=“@drawable” //设置垂直滚动条的drawable.  
android:scrollbarTrackHorizontal=“@drawable”//设置水平滚动条背景(轨迹)的色drawable  
android:scrollbarTrackVertical=“@deawable” //设置垂直滚动条背景(轨迹)的drawable注意直接  
android:scrollbarAlwaysDrawHorizontalTrack=“true/false” //设置水平滚动条是否含有轨道  
android:scrollbarAlwaysDrawVerticalTrack=“true/false”  // 设置垂直滚动条是否含有轨道  
android:requiresFadingEdge=“none/horizontal/vertical” //定义褪色时滚动边缘  
android:nextFocusLeft=“@+id/” //Up键按下之后,哪一个控件获得焦点(被选中)  
android:nextFocusRight=“@+id/”   
android:nextFocusUp=“@+id/”   
android:nextFocusDown=“@+id/”   
android:nextFocusForward=“@+id/”   
android:clickable=“true/false”   
android:longClickable=“true/false”   
android:saveEnabled=“true/false” //设置是否在窗口冻结时(如旋转屏幕)保存View的数据  
android:drawingCacheQuality=“auto|low|hight” //设置绘图缓存质量   
android:keepScreenOn=“true/false” //View在可见的情况下是否保持唤醒状态  
android:duplicateParentState=“true/false”  如果设置此属性,将直接从父容器中获取绘图状态(光标,按下等)。 注意根据目前测试情况仅仅是获取绘图状态,而没有获取事件,也就是你点一下LinearLayout时Button有被点击的效果,但是不执行点击事件。  
android:minHeight=“”   
android:minWidth=“”   
android:soundEffectsEnabled=“true/false” // 设置点击或触摸时是否有声音效果  
android:hapticFeedbackEnabled=“true/false” // 实现单击某个视图,系统提供一个触力反馈(震动一下)  
android:contentDescription=“@string/”        //图片不可见时的文字描述(盲人)   
android:onClick=“”   
android:overScrollMode=“ifContentScrolls/always/never” //滚动到边界时的效果   
android:alpha=“0.1” //透明度  
android:translationX=“” //X轴的偏移距离  
android:translationY=“”   
android:transformPivotX=“” //从某点的X轴偏移距离  
android:transformPivotY=“”   
android:rotation=“”  //旋转  
android:rotationX=“”   
android:rotationY=“”   
android:scaleX=“” //设置X轴缩放比例  
android:scaleY=“”   
android:verticalScrollbarPosition=“defaultPosition/left/right”  //设置垂直滚动条的位置  
android:layerType=“none/hardware/software” //绘图是否开启硬件加速  
android:layoutDirection=“” //定义布局图纸的方向  
android:textDirection=“”  //  
android:textAlignment=“inherit/…..”   //文字对齐方式  
android:importantForAccessibility=“noHideDescendants/….”    //设置可达性的重要行  
android:accessibilityLiveRegion=“”   
android:labelFor=“”   //添加标签  
android:measureAllChildren=“”  //见下  
android:animateLayoutChanges=“true”   //添加默认布局动画  
android:clipChildren=“” //见下  
android:clipToPadding=“” //见下转载  
android:layoutAnimation=“” //设置layout动画  
android:animationCache=“”  
android:persistentDrawingCache=“” android:alwaysDrawnWithCache=“” />
菜单资源
元素 说明
menu :在以前有物理菜单按钮,即menu键的手机上,用的较多
value目录
元素 说明
demens.xml 定义尺寸资源
string.xml 定义字符串资源
styles.xml 定义样式资源
colors.xml 定义颜色资源
arrays.xml 定义数组资源
attrs.xml 自定义控件时用的较多,自定义控件的属性

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activitytest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:exported="false">
        <intent-filter>
            <action android:name="com.example.activitytest.ACTION_START" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="com.example.activitytest.MY_CATEGORY" />
        </intent-filter>
        </activity>
    </application>

</manifest>

解析:


AndroiManifest.xml.png
  • 更改应用显示的名称:
    android:label="@string/app_name" ctrl + 左键跟踪到strings.xml,更改名称就可以

  • 更改应用图标:
    android:icon="@mipmap/ic_launcher"ctrl + 左键,跟踪到ic_launcher.xml,更改如下

<background android:drawable="@drawable/displattest" />
 <foreground android:drawable="@drawable/displattest" />

将相应的图标文件放到 drawable下面就可以

参考目录

Android工程相关解析(各种文件,资源访问)

相关文章

网友评论

      本文标题:Android APP学习(2)---- 基础demo 和工程文

      本文链接:https://www.haomeiwen.com/subject/fddwdrtx.html