目錄
一、文本控件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" />
