res
: 存放资源drawable
: 图片layout
: 布局文件mipmap-hdpi
: logo图片values
: 颜色、文字AndroidMainfest.xml
: 应用里面用到的所有内容,都需要在这个里面注册一下[部分代码说明]
src/main/java/com.skypan.helloworld/MainActivity
内的函数setContentView(R.layout.activity_main)
:表示,使用了activity_main布局
activity_main.xml
,将标签名改为如下:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" />
LinearLayout>
TextView android:text="Hello World"
: 显示在手机上的值为Hello World[最常用属性]:
android:id
: 控件的idandroid:layout_width
: 布局的宽度android:layout_height
: 布局的高度android:background
: 布局的背景颜色android:layout_margin
: 布局的左右边距android:layout_padding
: 布局的内侧边距android:orientation
: 布局的方向dp
: 根据屏幕自己算出大小match_parent
: 匹配父元素
: 是所有控件的父类,如
android: gravity= "bottom"
: 控件子元素的排列方式weight
: 权重,占父元素宽度(剩余)的权重.相当于flex布局
中子元素的属性flex:1
; 把剩余内容按照权重去分配[小结]:
/app/src/main/java/com.skypan.helloworld/MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate (saveInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
setContentView
使用到了activity_main
:总体的布局样式[最常见属性]:
android:layout_toLeftOf
: 在谁左边android:layout_toRightOf
: 在谁右边android:layout_alignBottom
: 跟谁底部对齐android:layout_alignParentBottom
: 跟父元素底部对齐android:layout_below
: 在谁的下面[栗子]:
<RelativeLayout>
<View
android:id="@+id/view_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#000000"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<RelativeLayout>
<View
android:id="@+id/view_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#000000"
/>
<View
android:id="@+id/view_2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#FF0033"
android:layout_toRightOf="@id/view_1"
/>
</RelativeLayout>
[栗子]:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"/>
</LinearLayout>
// MainActivity.java
import andoridx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
// 声明 button
private Button mBtnTextView;
@override
pretected void onCreate(Bundle saveInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnTextView = (Button) findViewById(R.id.btn_textview);
mBtnTextView.setOutClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到TextView演示界面
Intent intent = new Intent(packageContext: MainActivity.this, TextViewActivity.class);
startActivity(intent);
}
})
}
}
AndroidMainfest.xml
配置如下:<application
android:allowBckup="true"
...
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
>
android:id="@+id/tv_4"
// TextViewActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import android.os.Bundle;
public class TextViewActivity extends AppCompatActivity {
private TextView mTv4;
@override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view);
// 获取 id.tv_4
mTv4 = (TextView) findViewById(R.id.tv_4);
// 给 id.tv_4划线
mTv4.getPaint().setFlags(Paint.STRING_THRU_TEXT_FLAG);
// 去锯齿
mTv4.getPaint().setAntiAlias(true)
}
}
<Button
android:id="@+id/btn_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginTop="10dp" />
// MainActivity.java
// 声明控件
private Button mBtnButton
// 找到按钮 btn_button
mBtnButton = (Button) findViewById(R.id.btn_button);
// 给 mBtnButton 添加点击事件
mBtnButton.setOnClickListener(new View.OnClickListener(){
// 点击按钮后执行的操作
})
import andoirdx.appcompat.app.AppCompatActivity;
import andoird.widget.Button;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
// 声明button
private Button mBtnButton;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 找到按钮
mBtnButton = (Button) findViewById(R.id.btn_button);
mBtnButton.setOnClickListener(new View.OnClickListener() {
@ovrride
public void onClick(View v) {
// 跳转到 Button 演示界面
Intent intent = new Intent( packageContext: MainActivity.this, ButtonActivity.class);
startActivity(intent);
}
})
}
}
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#ffffff" />
<corners
android:radius="15dp" />
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#cc7c00" />
<corners android:radius="15dp" />
shape>
item>
<item android:state_pressed="false">
<shape>
<solid android:color="#ff9900">
<corners android:radius="15dp" />
shape>
item>
selector>
activity_button.xml
<Button
android:id="@+id/btn_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_3"
android:layout_marginTop="5dp"
android:text="Button 4"
android:textSize="20sp"
android:textColor="#0066ff"
android:background="@drawable/bg_btn4" />
activity_button.xml
<Button
...
android:onClick="showToast" />
ButtonActivity.java
中加入如下:public void showToast(View view) {
Toast.makeText(context: this, text:'点击', Toast.LENGTH_SHORT).show();
}
[报错]:
Could not find method showToast(View) in a parent or ancestor Context for android
: 写按钮触发事件的时候,没有传入参数 View view
,将public void showToast()
改为public void showToast(View view)
Button.setOnClickListener()
activity_button.xml
<Button
android:id="@+id/btn_3"
....
/>
ButtonActivity.java
中加入如下:import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
public class ButtonActivity extends AppCompatActivity {
private Button mBtn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBtn3 = (Button) findViewById(R.id.btn_3);
mBtn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ButtonActivity.this, 'btn3被点击', Toast.LENGTH_SHORT).show();
}
})
}
}
activity_button.xml
,如下:<TextView
android:id="@+id/tv_1"
...
/>
ButtonActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
public class ButtonActivity extends AppCompatActivity {
private TextView mTv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// tv_1的点击事件
mTv1 = (TextView) findViewById(R.id.tv_1);
mTv1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(ButtonActivity.this, "TextView被点击", Toast.LENGTH_SHORT).show();
}
})
}
}
activity_main.xml
<Button
android:id="@+id/btn_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EditText"
android:textAllCaps="false"/>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.context.Intent;
import android.widget.Button;
import android.view.View;
public class MainActivity extends AppCompatActivity {
// 声明button
private Button mBtnEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 找到 btn_edittext
mBtnEditText = (Button) findViewById(R.id.btn_edittext);
mBtnEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到 EditText 界面
// 这里先假设有 EditTextActivity
Intent intent = new Intent(MainActivity.this, EditTextActivity.class);
startActivity(intent);
}
});
}
}
activity_edit_text.xml
<RelativeLayout xmlns:android="http://schemas.android/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:padding="15dp">
<EditText
android:id="@+id/et_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:textColor="#ffad33"
android:hint="用户名"/>
<EditText
android:id="@+id/et_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/et_1"
android:hint="密码"
android:inputType="textPassword"
android:layout_marginTop="5dp"
/>
RelativeLayout>
activity_edit_text.xml
加样式<EditText
android:id="@+id/et_1"
...
android:background="@drawable/bg_username"
/>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
andorid:color="#999999" />
<corners
android:radius="5dp"/>