在java代码中引用资源,使用 R. 形式引用资源:
txtName.setText(getResources().getText(R.string.name));在 xml 中引用资源,使用 @ 引用资源:
<TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background = "@drawable/img_back"/>java文件。
package com.example.myapplication; 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);//设置布局文件 } }布局文件。
<?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"> <TextView android:id="@+id/textView" <!-- 创建资源文件id --> android:layout_width="wrap_content" <!-- 以原始大小显示,相对的还有充满容器 fill_parent --> android:layout_height="wrap_content" android:layout_marginBottom="40dp" <!-- 边界尺寸 --> app:layout_constraintStart_toStartOf="parent" android:text="@string/hello_world" <!-- 显示文字 --> /> </androidx.constraintlayout.widget.ConstraintLayout>项目维护文件。
<?xml version="1.0" encoding="utf-8"?> <!-- 版本及编码方式 --> <manifest xmlns:android="http://schemas.android.com/apk/res/android" <!-- 定义我们使用的架构,来自于... --> package="com.example.myapplication" <!-- 定义我们使用的java包,应用的唯一标识 --> > <application android:allowBackup="true" <!-- 允许备份文件 --> android:icon="@mipmap/ic_launcher" <!-- 启动图标 --> android:label="@string/app_name" <!-- app名称 --> android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <!-- 主题名称 --> <activity android:name=".Main2Activity"></activity> <!-- 第二个活动页 --> <activity android:name=".MainActivity"> <!-- 第一个活动页 --> <intent-filter> <!-- 意图过滤器 --> <action android:name="android.intent.action.MAIN" /> <!-- 这两行代码代表程序入口 --> <category android:name="android.intent.category.LAUNCHER" /> <!-- 这两行代码代表程序入口 --> </intent-filter> </activity> </application> </manifest>Android APP都需要我们用一个证书对应用进行数字签名,不然的话是无法安装到Android手机上的,平时我们调试运行时到手机上时,是AS会自动用默认的密钥和证书来进行签名;但是我们实际发布编译时,则不会自动签名,这个时候我们就需要进行手动签名了! 为我们的APK签名有以下好处:
应用程序升级:如果你希望用户无缝升级到新的版本,那么你必须用同一个证书进行签名。这是由于只有以同一个证书签名,系统才会允许安装升级的应用程序。如果你采用了不同的证书,那么系统会要求你的应用程序采用不同的包名称,在这种情况下相当于安装了一个全新的应用程序。如果想升级应用程序,签名证书要相同,包名称要相同!应用程序模块化: Android系统可以允许同一个证书签名的多个应用程序在一个进程里运行,系统实际把他们作为一个单个的应用程序,此时就可以把我们的应用程序以模块的方式进行部署,而用户可以独立的升级其中的一个模块。代码或者数据共享: Android提供了基于签名的权限机制,那么一个应用程序就可以为另一个以相同证书签名的应用程序公开自己的功能。以同一个证书对多个应用程序进行签名,利用基于签名的权限检查,你就可以在应用程序间以安全的方式共享代码和数据了。 不同的应用程序之间,想共享数据,或者共享代码,那么要让他们运行在同一个进程中,而且要让他们用相同的证书签名。https://www.runoob.com/w3cnote/android-tutorial-sign-package.html
Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局) FrameLayout(帧布局),AbsoluteLayout(绝对布局),GridLayout(网格布局) 。
初学者对于这两个属性可能会有一点混淆,这里区分下: 首先margin代表的是偏移,比如marginleft = "5dp"表示组件离容器左边缘偏移5dp; 而padding代表的则是填充,而填充的对象针对的是组件中的元素,比如TextView中的文字 比如为TextView设置paddingleft = "5dp",则是在组件里的元素的左边填充5dp的空间! margin针对的是容器中的组件,而padding针对的是组件中的元素,要区分开来!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/btn1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button"/> <Button android:paddingLeft="100dp" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_toRightOf="@id/btn1"/> <Button android:id="@+id/btn2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_alignParentBottom="true"/> <Button android:layout_marginLeft="100dp" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_toRightOf="@id/btn2" android:layout_alignParentBottom="true"/> </RelativeLayout>FrameLayout的属性很少就两个,但是在说之前我们先介绍一个东西:
前景图像:永远处于帧布局最上面,直接面对用户的图像,就是不会被覆盖的图片。
两个属性:
android:foreground:*设置改帧布局容器的前景图像android:foregroundGravity:设置前景图像显示的位置 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/FrameLayout1" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:foreground="@drawable/logo" android:foregroundGravity="right|bottom"> <TextView android:layout_width="200dp" android:layout_height="200dp" android:background="#FF6143" /> <TextView android:layout_width="150dp" android:layout_height="150dp" android:background="#7BFE00" /> <TextView android:layout_width="100dp" android:layout_height="100dp" android:background="#FFFF00" /> </FrameLayout>和前面所学的TableLayout(表格布局) 有点类似,不过他有很多前者没有的东西,也更加好用。
可以自己设置布局中组件的排列方式可以自定义网格布局有多少行,多少列可以直接设置组件位于某行某列可以设置组件横跨几行或者几列而 TableLayout 的列数取决于你添加组件的个数。
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/GridLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:columnCount="4" android:orientation="horizontal" android:rowCount="6" > <TextView android:layout_columnSpan="4" android:layout_gravity="fill" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#FFCCCC" android:text="0" android:textSize="50sp" /> <Button android:layout_columnSpan="2" android:layout_gravity="fill" android:text="回退" /> <Button android:layout_columnSpan="2" android:layout_gravity="fill" android:text="清空" /> <Button android:text="+" /> <Button android:text="1" /> <Button android:text="2" /> <Button android:text="3" /> <Button android:text="-" /> <Button android:text="4" /> <Button android:text="5" /> <Button android:text="6" /> <Button android:text="*" /> <Button android:text="7" /> <Button android:text="8" /> <Button android:text="9" /> <Button android:text="/" /> <Button android:layout_width="wrap_content" android:text="." /> <Button android:text="0" /> <Button android:text="=" /> </GridLayout>略
Todo.
用于显示文本。
<TextView android:id="@+id/txtOne" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="TextView(显示框)" android:textColor="#EA5246" android:textStyle="bold|italic" android:background="#000000" android:textSize="18sp" />id:为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置,又或者使用RelativeLayout时,参考组件用的也是id!
layout_width:组件的宽度,一般写:wrap_content或者match_parent(fill_parent),前者是控件显示的内容多大,控件就多大,而后者会填满该控件所在的父容器;当然也可以设置成特定的大小,比如我这里为了显示效果,设置成了200dp。
layout_height:组件的高度,内容同上。
gravity:设置控件中内容的对齐方向,TextView中是文字,因此这里是文字对齐ImageView中是图片等等。
text:设置显示的文本内容,一般我们是把字符串写到string.xml文件中,然后通过@String/xxx取得对应的字符串内容的,这里为了方便我直接就写到""里,不建议这样写!!!
textColor:设置字体颜色,同上,通过colors.xml资源来引用,别直接这样写!
textStyle:设置字体风格,三个可选值:normal(无效果),bold(加粗),italic(斜体)
textSize:字体大小,单位一般是用sp!
background:控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片哦!
android:shadowColor:设置阴影颜色,需要与shadowRadius一起使用哦!
android:shadowRadius:设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0
android:shadowDx:设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置
android:shadowDy:设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置
更多属性: https://www.runoob.com/w3cnote/android-tutorial-textview.html
android:textAllCaps 为禁止大小写转换。
调用:
Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 在此处添加逻辑 } });android:hint:默认显示文字 android:textColorHint:默认显示文字的颜色 android:maxLines:最大显示行数,当输入的内容超过两行时,文本就会向上滚动
使用方法:
EditText edit_one = (EditText) findViewById(R.id.edit_one); String str=edit_one.getText().toString(); //获取文本 edit_one.getText().insert("124");//插入文本显示图像的控件。支持xml和png格式。
<ImageView android:id="@+id/image_view" android:layout_width="200dp" android:layout_height="100dp" android:src="@drawable/test" />android:src:图片位置。
src属性和background属性的区别: background通常指的都是背景,而src指的是内容!! 当使用src填入图片时,是按照图片大小直接填充,并不会进行拉伸 而使用background填入图片,则是会根据ImageView给定的宽度来进行拉伸
使用:
imageView.setImageResource(R.drawable.img_2);圆形进度条:
<ProgressBar android:id="@+id/progress_bar" android:layout_width="match_parent" android:layout_height="wrap_content" />条形进度条:
<ProgressBar android:id="@+id/progress_bar" android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:max="100" />可以使用 progressBar.setProgress(progress); 来设置进度。
visible、invisible 和gone 。visible 表示控件是可见的,这个值是默认值,不指定android:visibility 时,控件都是可见的。invisible 表示控件不可见,但是它仍然占据着原来的位置和大小,可以理解成控件变成透明状态了。gone 则表示控件不仅不可见,而且不再占用任何屏幕空间。
ProgressBar progressBar=(ProgressBar) findViewById(R.id.progress_bar); progressBar.setVisibility(View.GONE);//控件不可见并消除弹出一个对话框。
AlertDialog.Builder dialog = new AlertDialog.Builder (MainActivity.this); dialog.setTitle("This is Dialog"); dialog.setMessage("Something important."); dialog.setCancelable(false); dialog.setPositiveButton("OK", new DialogInterface. OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.setNegativeButton("Cancel", newDialogInterface. OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.show();ProgressDialog会在对话框中显示一个进度条,一般用于表示当前操作比较耗时,让用户耐心地等待。
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setTitle("This is ProgressDialog"); progressDialog.setMessage("Loading..."); progressDialog.setCancelable(true); progressDialog.show();CustomeView.java
package com.example.myapplication; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; /** * 项目名称:changecountview * 类描述: * 创建人:Administrator * 创建时间:2015/12/11 16:37 * 修改人:Administrator * 修改时间:2015/12/11 16:37 * 修改备注: */ public class CustomeView extends View{ private Paint mPaint=new Paint(); private Path path=new Path(); private float degress=90; public CustomeView(Context context) { super(context); } public CustomeView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomeView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { //绘制最外层大圆 mPaint.setColor(Color.BLACK);//设置画笔颜色为黑色 mPaint.setStyle(Paint.Style.FILL_AND_STROKE);//设置画笔style实心 RectF rect= new RectF(getWidth() / 2 - getHeight() / 2, 0, getWidth() / 2 + getHeight() / 2, getHeight());//圆弧的外接矩形 canvas.drawArc(rect, 270, 180, false, mPaint); mPaint.setColor(Color.WHITE);//设置画笔颜色为白色 canvas.drawArc(rect, 90, 180, false, mPaint); //绘制中间层上边圆 mPaint.setColor(Color.BLACK); rect= new RectF(getWidth()/2-getHeight()/4,0,getWidth() / 2 + getHeight() / 4, getHeight() /2); canvas.drawArc(rect, 90, 180, false, mPaint); //绘制中间层下边圆 mPaint.setColor(Color.WHITE); rect= new RectF(getWidth()/2-getHeight() / 4, getHeight() / 2, getWidth() / 2 + getHeight() / 4, getHeight()); canvas.drawArc(rect, 270, 180, false, mPaint); //绘制最上层白色小圆 mPaint.setColor(Color.WHITE); canvas.drawCircle(getWidth() / 2, getHeight() / 4, getHeight() / 10, mPaint); //绘制最上层黑色小圆 mPaint.setColor(Color.BLACK); mPaint.setStyle(Paint.Style.FILL); canvas.drawCircle(getWidth() / 2, getHeight() * 3 / 4, getHeight() / 10, mPaint); } }activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.myapplication.CustomeView android:layout_width="match_parent" android:layout_height="250dp"> </com.example.myapplication.CustomeView> </LinearLayout>参考链接: https://www.runoob.com/w3cnote/android-tutorial-intro.html