学习Android的过程中,很多细节又常用的操作不熟练,再此记录一下。
获取当前Activity 名称
adb shell "dumpsys window | grep mCurrentFocus"
1.设置背景渐变色、圆角等
1.创建 shape文件来实现 定制化形状的XML文件
通过创建一个shape文件来实现。在app目录的res->drawable下。
2.渐变色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#F5F6F7" //开始颜色
android:endColor="#0ABCA1" // 结束颜色
android:angle="90" // 渐变防线[角度]
android:type="linear" // 渐变类型(线性渐变)
/>
</shape>
3.圆角渐变色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 渐变色 -->
<gradient
android:startColor="#12C393"
android:endColor="#0ABCA1"
android:angle="0"
android:type="linear"
/>
<!-- 圆角 -->
<corners android:radius="22dp"/>
</shape>
4.圆角边框
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" /> <!-- 背景填充颜色 -->
<corners android:radius="18dp" /> <!-- 圆角半径,可根据需求调整 -->
<stroke
android:width="1dp"
android:color="#E6E7E8" /> <!-- 边框宽度、颜色 -->
</shape>
5.填充色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp"/>
<solid android:color="@color/color_7D9D98"/>
</shape>
需要区分的是
solid和stock,容易混淆
您可以将上述代码保存为一个名为gradient_bg.xml的文件,并将其放置在res/drawable目录下。因此,渐变色通常通过drawable XML文件来定义,并且可以在布局文件中通过android:background属性来引用和应用。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_bg" />
在Android中,渐变角度指定了渐变色的方向或角度。它用于定义渐变色的起始点和结束点之间的渐变方向。渐变角度的取值范围是0到360度,其中0度表示从左到右,90度表示从上到下,以此类推。
以下是一些常见的渐变角度值及其对应的方向:
android:angle属性
- 0度:从左到右
- 45度:从左上到右下
- 90度:从上到下
- 135度:从右上到左下
- 180度:从右到左
- 225度:从右下到左上
- 270度:从下到上
- 315度:从左下到右上
android:type属性
1. `linear`(线性渐变):使用直线作为渐变的参考线。默认类型为线性渐变。
1. `radial`(径向渐变):以中心点为基础,向外辐射状地创建渐变。
1. `sweep`(扫描渐变):以中心点为起点,按顺时针方向创建扇形渐变。
drawable中的shape文件还可以对图片进行修饰,后续补充.
2.AS直接添加本地Java文件
复制本地Java文件到对应的文件夹
【Finder】中即可,AS会自动刷新出来,如果导入的该Java文件中引用的部分类,是其他工程的,当前工程中没有,则会爆红。
3.cardView
a.cardview使用app:属性
需要添加
xmlns:app依赖
举例
xmlns:app="http://schemas.android.com/apk/res-auto"
b.cardview背景
在cardView 中设置背景色,如果直接设置android:background="@color/black"是无效的,需要设置
cardview圆角属性:app:cardCornerRadius="33dp"后续添加
app:cardBackgroundColor="@color/black"
需要注意的是:如果是设置单个颜色可以在工程
colors.xml文件中新增一个单个颜色值,如果是需要添加渐变色。则只能通过设置其子布局的背景色来实现效果。如下代码所示,设置了子布局LinearLayout的background设置为shape.xml文件来设置渐变色,曲线救国。
<androidx.cardview.widget.CardView
android:layout_width="66dp"
android:layout_height="66dp"
app:cardCornerRadius="33dp"
android:layout_marginBottom="30dp"
android:id="@+id/addItem"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/app_add_gradient_weeklyreport"
android:orientation="vertical"
>
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:src="@mipmap/ic_weekly_add"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textStyle="normal"
android:text="添加"
android:textColor="@color/white"
android:textSize="14sp"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
4.EditText控件
如下所示,为一个没有背景的EdiText控件,且有placeholder字体和颜色,
padding属性可以设置默认文字距离控件边距,
gravity来设置默认文字的位置
maxLength可输入的最大字数
setEnable(bool)是否可编辑
.getText().toString编辑框值
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:background="@drawable/app_round_10_e2e2e2">
<EditText
android:layout_width="match_parent"
android:layout_height="150dp"
android:hint="请输入"
android:textColorHint="#BABABA"
android:textColor="#242629"
android:textSize="15sp"
android:textStyle="bold"
android:background="@null"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:gravity="top"
android:maxLength="100"
android:id="@+id/findquest_edit_id" />
</LinearLayout>
5.在线性布局下,如果需要子控件垂直分布,需要与父布局垂直方向设置,如下LinearLayout设置vertical,imageview位于父布局的上方且居中,设置center_horizontal,如果设置center_vertical,则无效
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/app_add_gradient_weeklyreport"
android:orientation="vertical"
>
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:src="@mipmap/ic_weekly_add"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textStyle="normal"
android:text="添加"
android:textColor="@color/white"
android:textSize="14sp"
/>
</LinearLayout>
6.Recycleview中item的高度一般在layout.xml文件中设置,默认是自适应高度,应该会因为设置了固定高度或者布局影响
android中的列表高度是可以设置Recycleview的高度自适应match_parent或者warp_content,只需要在itme.xml布局文件中把能设置的高度设置即可,很简单,因为本身都是自适应的。
7.自闭合标签和【开始、结束标签】
在 Android 布局文件中,可以使用两种方式来闭合标签:
-
使用自闭合标签:在标签的末尾使用
/符号来表示标签的结束,例如android:layout_width="match_parent" />。这种方式适用于没有子视图的标签,如<ImageView />或<TextView />。 -
使用开始和结束标签:在标签的开始和结束两端使用完整的开始标签和结束标签,例如
<LinearLayout>和</LinearLayout>。这种方式适用于包含子视图的标签,你可以在开始标签和结束标签之间添加其他标签或视图。
需要注意的是,对于某些标签,如 <LinearLayout>、<RelativeLayout> 等,如果没有子视图,可以使用自闭合标签或开始和结束标签都可以。这是因为这些标签被定义为可以包含子视图,但也可以为空。
以下是使用自闭合标签和开始和结束标签的示例:
<!-- 使用自闭合标签 -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image" />
<!-- 使用开始和结束标签 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World" />
</LinearLayout>
在上述示例中,ImageView 标签使用了自闭合标签,因为它没有子视图。而 LinearLayout 标签使用了开始和结束标签,因为它包含了一个 TextView 子视图。
总而言之,你可以根据标签是否包含子视图来选择使用自闭合标签或开始和结束标签。对于没有子视图的标签,可以使用自闭合标签,而对于包含子视图的标签,应该使用开始和结束标签。
8.对于三大常用布局线性布局、相对布局、约束布局(这个也用得不多)属性的总结
a.相对布局
1.相对父视图布局 ParentRight..
2.相对某个控件布局 toLeftOfxx above(在xx之上) below(在。。之下) (上下 和 左右)
b.线性布局
1.orientation设置 控件的排列方式 水平或者垂直
2.weight 宽或高比列分配线性布局中使用较多,使用该属性,有约束关系的控件中部分控件宽或者高需设置为0db,否则无效
3.其中还可以设置layout_gravity: top bottom center_xx 等属性
c.约束布局
1.拖动控件即可关键的属性bias属性设置比列
2.如果设置了weight(权重),宽或者高设置为0dp的会按照 weight 的比例分配宽或高
app:layout_constraintVertical_bias="0.1"
app:layout_constraintVertical_weight="1"
对于Padding、layout_Margin等属性都是公共属性,任何一种布局都有
参考,布局就是经常练习,自然就会了。看到比较复杂的、或者有一套自己总结规律的,不然记录没有意义。
9.xml布局文件中的单位
在xml文件中常见的width、height的单位使用dp,对于fontSize的单位使用sp,其中特殊的就是在cardview中使用
app:cardElevation="0px"
来去掉边框阴影
10.添加xml文件
11.Android studio 添加图片资源
选择-hdpi、-mdpi、-xhdpi 三种之一,最好是对应UI资源。如果不能对应,直接选 -xhdpi
同时,如果拖动两张图就会自动生成一个图片名称的文件夹
11.布局中,部分控件固定在底部
其实很简单,给上方元素添加下方代码即可
android:layout_weight="1"