Android_基础控件


目录

  一、文本控件TextView

  二、按钮控件Button

  三、图片控件ImageView

  四、输入控件EditText

 

一、文本控件TextView

1.布局文件

<TextView android:id="@+id/tv_show" android:text="@string/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/green" android:textSize="@dimen/title" android:lines="1" android:maxWidth="40dp" android:ellipsize="middle" android:focusable="true" android:focusableInTouchMode="true"
/>

2.控件属性

android:id 控件唯一标识
android:text 显示的文本信息
android:layout_width 控件宽度
android:layout_height 控件高度
android:textSize 字体大小
android:textColor 字体颜色
android:lines 文本显示行数
 android:maxWidth 最大显示宽度
android:ellipsize
设置当文本过长时如何显示文本内容
start:省略号显示在开头
middle:省略号显示在中间
end:省略号显示在结尾
marquee:以跑马灯方式显示
 
android:focusable
是否获得焦点
android:
focusableInTouchMode
触摸模式后是否可获得焦点

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.对象获取

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取文本对象
    TextView tv_show = (TextView) findViewById(R.id.tv_show); //获取android:text属性值
    String text = tv_show.getText().toString(); //后台日志输出
    Log.i("wl",text); //设置android:text
    tv_show.setText("Hello Man"); //通过getResources()获得资源常量
 tv_show.setTextColor(getResources().getColor(R.color.colorPrimary)); //吐司 在app中输出
    Toast.makeText(this,text,Toast.LENGTH_LONG).show(); }

 

 二、按钮控件Button

1.布局文件

<Button android:id="@+id/btn_show" android:text="按钮" android:textSize="20sp" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btnClick"
/>

 

2.注册监听

 

(1)匿名内部类

//获取按钮对象
Button btn_show = (Button) findViewById(R.id.btn_show); //注册点击监听 
btn_show.setOnClickListener(new View.OnClickListener() {   @Override public void onClick(View v) {   Toast.makeText(MainActivity.this,"点击按钮",Toast.LENGTH_LONG).show();   } }); 

 

(2)接口实现

public class MainActivity extends AppCompatActivity implements View.OnClickListener { //声明控件对象
 Button btn_show ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取按钮对象
        btn_show = (Button) findViewById(R.id.btn_show); //注册点击监听
        btn_show.setOnClickListener(this); } //实现接口类
 @Override public void onClick(View v) { Toast.makeText(this,"点击按钮",Toast.LENGTH_LONG).show(); } }

 

(3)设置onclick属性

public void btnClick(View v){ Toast.makeText(this,"点击按钮",Toast.LENGTH_LONG).show(); }

 

3.按钮背景图片设置及点击效果

 

(1)在res/drawable下创建btn_selector.xml,选择选中和没选中时的背景图片

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_bg2" android:state_pressed="false"/>
    <item android:drawable="@drawable/btn_bg_p" android:state_pressed="true"/>
</selector>

 

(2)按钮布局文件中背景图片使用btn_selector.xml

<Button android:text="卸载" android:layout_width="100dp" android:layout_height="50dp" android:background="@drawable/btn_selector" android:textColor="#fff" android:textSize="18sp"
/>

 

 

 三、图片控件ImageView

1.布局文件

<ImageView android:src="@drawable/danger" android:background="@drawable/danger" android:layout_width="wrap_content" android:layout_height="wrap_content" />

 

 

2.控件属性

android:src
设置ImageView中显示的图片
– 是前景,显示在前面
– 可根据宽高缩放,但是保持图片原有比例 
android:background
设置ImageView控件的背景
– 是背景,显示在后面
– 可根据宽高缩放,但是不保持图片原有比例
– 除了图片以外,背景还可以是颜色 

 

 

 

 

 

 

 

 

3.图片资源

(1)注意命名中不得含有中文或大写字母
(2)首字母必须以字母开头
(3)格式png,jpg
 
 

四、输入控件EditText

1.布局文件

<EditText android:hint="请输入" android:layout_width="match_parent" android:layout_height="wrap_content" />

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM