文件架构
mainifests: 运行配置文件,图标、名字、权限。。。。。。
java: 源文件
content_main——代码文件,组件属性描述
activity_main——布局文件,组件位置描述
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-79LUMCAt-1669884642102)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20221127155622991.png)]
Activity
可视化窗口,可以放置UI组件进行交互,一个Activity.java与一个Layout.xml对应,自带一个mainActivity,所有Activity都要在AndroidManifests里进行注册
<activity android:name=".MainActivity"></activity>
<activity
android:name=".Main2Activity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
视图、布局、控件
视图类view派生了所有的布局和控件,布局是个容器,可以装其他布局和控件,控件则不行,view类常用的属性定义如下
- id
- layout_width
- layout_height
- layout_margin
- layout_mariginTop/Bottom/Left/Right
- minWidth
- minHeight
- background 颜色或图片
- layout_gravity 该视图与上级视图的对齐方式
- padding 该视图边缘与内部内容的空白距离 paddingLeft/Right/Top/Bottom
- visibility
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cdCGIBak-1669884642105)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20221127162053864.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3LFEzuD0-1669884642106)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20221127162158021.png)]
Click、listener
click事件要选择监听对象,监听对象要么本身就是View的派生类,要么就继承一个View.OnClickListener类,再重写OnClick()函数,监听者可能监听多个对象,收到Click事件后需要判断是哪个对象被click了
常见的点击事件:
Button = 被观察对象
OnClickListner = 观察者
setOnClickListner() = 订阅
OnClick() = 事件
一个Activity作为Listener,onClick()函数传回view的
@Override
public void onClick(View v){
if(v.getId() == R.id.button){
}
}
语法
findViewById(R.id.name)
@color/常量名 //使用
android:id="@+id/textView" // 定义
<TextView
android:id="@+id/textView"
TextView Methods
setText("")
setTextColor(Color.Red)
setTextSize(30)
<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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
//布局声明 <layout code> </layout>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="592dp"
android:text="Halo! Gy"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
app:fontFamily="monospace"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:textAllCaps="false" />
//控件声明 <view/> 斜杠表示自闭合,不再下放视图或控件
</androidx.constraintlayout.widget.ConstraintLayout>
//视图声明结尾 </layout>
水平滚动条
滚动视图下面先放个布局再放其他视图
<HorizontalScrollView
android:layout_width = "wrap_content"
android:layout_height = "200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:orientation = "horizontal">
<LinearLayout
android:layout_height = "match_parent"
android:layout_width = "wrap_content"
android:orientation = "horizontal"
>
<View
android:layout_height = "match_parent"
android:layout_width = "400dp"
android:background = "#F12F45"
/>
<View
android:layout_height = "match_parent"
android:layout_width = "400dp"
android:background = "#F12FF5"
/>
</LinearLayout>
</HorizontalScroolView>
TextView
chatLog.setLines(4);
chatLog.setMaxLines(10);
chatLog.setMovementMethod(new ScrollingMovementMethod());
Toast
轻量级消息提醒,第一个参数设置context,一般为所在类.this,第三个参数选择显示时长
Toast.makeText(Context, String, LENGTH_SHORT/LENGTH_LONG).show();
Toast.makeText(MainActivity.this,"ChatLog Renewed",Toast.LENGTH_SHORT).show();
ImageView
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tHKJLhDA-1669884642106)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20221128115909565.png)]
其他
颜色
Android里颜色由透明度alpha和RGB构成,有八位和六位两种编码方法
-
八位 0xFF00FF00
前两位表示透明度,后面每两位依次分别表示RGB
-
六位 0x00ff00
默认透明