控件 TextView 显示文本
Plain Text 输入文本
//将布局xml文件引入到activity当中
setContentView(R.layout.activity_main);
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="28sp"
android:textColor="#eeeeee"
tools:layout_editor_absoluteX="57dp"
tools:layout_editor_absoluteY="32dp" />
<EditText
android:hint="请输入你的姓名"
android:textSize="20sp"
android:id="@+id/editText"
android:layout_width="266dp"
android:layout_height="43dp"
tools:layout_editor_absoluteY="99dp"
tools:layout_editor_absoluteX="59dp" />
针对虚拟机控件集中在左上角:
是布局设置的不对,如LinearLayout
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.adb.li806.myapplication.MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:background="#f0f0f0"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#f0f0f0" />
LinearLayout>
不同分辨率图片设置
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Button_name" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="77dp"
android:layout_weight="0.13"
android:background="@drawable/doraemon1"
android:src="@mipmap/ic_launcher" />
LinearLayout>
Button 可以设置文本内容的按钮
ImageButton 不可以设置文本内容,background以及src添加一个image,当前图片上可以做一个有文本的图片(何苦内“—”)
设置background图片会填充整个ImageButton,设置src图片会自适应
以上文件都是在修改activity_main.xml文件
package com.adb.li806.myapplication;
import android.media.Image;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button loginButton;
private Button bt2;
private ImageButton ibt1;
private Button bt3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//将布局xml文件引入到activity当中
setContentView(R.layout.activity_main);
/*
* 1.初始化当前所需要控件,如何初始化一个控件?
* findViewById 返回的是一个View的对象,
* View是所有控件的父类
* findViewById是如何查找到对应view的id? R.java文件生成控件唯一的id
*
* 2.设置Button的监听器,通过监听器实现我们点击Button要操作的事情
*
*/
loginButton = (Button) findViewById(R.id.button1);
//(1)监听事件通过第一种方式实现(匿名内部类)
// 该方法需要一个OnClickListener对象
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//在当前onClick方法中监听点击Button的动作
System.out.println("我的Button被点击了");
}
});
//(2)外部类监听点击事件(在开发中比较少见)
bt2 = (Button) findViewById(R.id.button2);
ibt1 = (ImageButton) findViewById(R.id.imageButton1);
bt2.setOnClickListener(new MyOnClickListener(){
//手动实现onClick方法
@Override
public void onClick(View v) {
super.onClick(v);//调用父类(MyOnClickListener)的onClick事件
Toast.makeText(MainActivity.this,"bt2要执行的逻辑", Toast.LENGTH_SHORT ).show();
//MainActivity.this 指明调用的是谁
//Toast 是一个 View 视图,消息框,主要用于 一些帮助 / 提示。
// 第一个参数:当前的上下文环境。可用getApplicationContext()或this
// 第二个参数:要显示的字符串。也可是R.string中字符串ID
// 第三个参数:显示的时间长短。Toast默认的有两个LENGTH_LONG(长)和LENGTH_SHORT(短),也可以使用毫秒如2000ms
}
});
ibt1.setOnClickListener(new MyOnClickListener(){
@Override
public void onClick(View v) {
super.onClick(v);
Toast toast=Toast.makeText(getApplicationContext(), "ibt1要执行的逻辑", Toast.LENGTH_SHORT);
//显示toast信息
toast.show();
}
});
//(3)通过实现一个接口的方式实现监听事件
bt3 = (Button) findViewById(R.id.button3);
bt3.setOnClickListener(this);
}
//接口方式监听按钮点击事件
@Override
public void onClick(View v) {
Log.i("tag","第三种方式实现");
//"tag":可以理解成标志,查看日志时快速找到自己设置的标志,以便查看text的内容,tag也恰恰是标志的意思
}
}
/*外部类
* OnClickListener是一个接口所以不能用继承 extends
* 错误示范:class MyOnClickListener extends View.OnClickListener
*外部类可以在com.adb.li806.myapplication包下重新写一个class
*
*
* 好处:多个按钮要输出同样一句话。(不仅要干自己的事情还要做同样的事情)
* 也就是通过外部类的书写减少代码的冗余。
*
*/
class MyOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
Log.i("tag","父类的onClick事件");
/*
* 1、Log.v 的调试颜色为黑色的,任何消息都会输出,这里的v代表verbose啰嗦的意思,平时使用就是Log.v("","");
* 2、Log.d的输出颜色是蓝色的,仅输出debug调试的意思,但他会输出上层的信息,过滤起来可以通过DDMS的Logcat标签来选择.
* 3、Log.i的输出为绿色,一般提示性的消息information,它不会输出Log.v和Log.d的信息,但会显示i、w和e的信息
* 4、Log.w的意思为橙色,可以看作为warning警告,一般需要我们注意优化Android代码,同时选择它后还会输出Log.e的信息。
* 5、Log.e为红色,可以想到error错误,这里仅显示红色的错误信息,这些错误就需要我们认真的分析,查看栈的信息了
* */
//让所有使用当前外部类的点击事件的按钮都要做出一个动作,改变button本身的透明 alpha 是0到1 0是不显式 1是显示
v.getBackground().setAlpha(100);//透明度0~255透明度值 ,值越小越透明
//错误:v.setAlpha(0.5f); // f 代表float类型 v点击按键的对象
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"
android:layout_weight="0.09" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Button_name"
android:layout_weight="0.13" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="77dp"
android:layout_weight="0.13"
android:background="@drawable/doraemon1"
android:src="@mipmap/ic_launcher" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第三人称"
android:layout_weight="0.10" />
LinearLayout>