Android基礎(chǔ)之常用控件屬性介紹
常用控件之常用屬性
控件可見性
android:visibility="visible/invisible/gone"
visible表示控件可見(默認)/invisible表示控件不可見/gone表示控件不可見且不再占用任何屏幕空間
TextView
android:layout_height/width(match_parent/wrap_content)前者由父布局決定,后者由控件內(nèi)容決定
android:gravity指定文字的對齊方式
android:textSize 指定文字的大小(sp單位)
android:textColor 指定文字顏色
Button
android:textAllCaps 是否將字母自動進行大寫轉(zhuǎn)換 默認true
EditText
android:hint="提示文字" 指定一段提示性文本
android:maxLines="2" 指定最大行數(shù)為2
ImageView
android:src="drawable/img_1" 指定一張圖片
ProgressBar
用于在屏幕上顯示進度條
style="?android:attr/progressBarStyleHorizontal" 通過style屬性設(shè)置成水平進度條
android:max="100" 通過max設(shè)定進度條最大值
AlertDialog
彈出對話框,置頂于所有界面元素之上,能夠屏蔽掉其他控件的交互能力
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = findViewById(R.id.button_useful); button.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.button_useful: AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle("This is dialog"); dialog.setMessage("Something important."); dialog.setCancelable(false); dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); dialog.setNegativeButton("cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); dialog.show(); break; default: break; } } }
ProgressDialog
和AlertDialog相類似,區(qū)別是,此控件會在對話框中顯示一個進度條
注意,如果progressDialog.setCancelable()填寫的是false,則你點擊back鍵無法取消掉,需要自行使用dismiss()來關(guān)閉對話框,否則該控件將會一直存在
case R.id.button_progress_dialog: ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setTitle("This is ProgressDialog"); progressDialog.setMessage("Loading..."); progressDialog.setCancelable(true); progressDialog.show(); break;
Toast
顯示文本
Toast.makeText(context,顯示的內(nèi)容,顯示的時間長短);
context--Activity實例或getApplicationContext()得到
時間長短有兩種:Toast.LENGTH_LONG/Toast.LENGHT-SHORT
最后.show()用來顯示文本
顯示圖片
在寫Android的XML布局文件時,在ImageView或ImageButton中經(jīng)常會碰到一個提示:
Missing contentDescription attribute on image.
這個屬性是做什么的呢?
這個屬性是方便一些生理功能有缺陷的人使用應(yīng)用程序的。比如我們有一個ImageView里面放置一張顏色復(fù)雜的圖片,可能一些色弱色盲的人,分不清這張圖片中畫的是什么東西。如果用戶安裝了輔助瀏覽工具比如TalkBack,TalkBack就會大聲朗讀出用戶目前正在瀏覽的內(nèi)容。TextView控件TalkBack可以直接讀出里面的內(nèi)容,但是ImageView TalkBack就只能去讀contentDescription的值,告訴用戶這個圖片到底是什么。
public void buttonToImage(View view) { Toast toast = new Toast(this); ImageView imageView = new ImageView(this); //設(shè)置圖片 imageView.setImageResource(R.drawable.head); //顯示圖片 toast.setView(imageView); toast.setDuration(Toast.LENGTH_LONG); //設(shè)置圖片位置 toast.setGravity(Gravity.TOP,0,0); toast.show(); }
顯示圖文
public void buttonToBoth(View view) { Toast toast = new Toast(this); TextView textView = new TextView(this); textView.setText("這是頭像"); textView.setGravity(Gravity.CENTER); textView.setTextSize(20); ImageView imageView = new ImageView(this); imageView.setImageResource(R.drawable.head); //組合 LinearLayout layout = new LinearLayout(this); //設(shè)置垂直 layout.setOrientation(LinearLayout.VERTICAL); //設(shè)置居中 layout.setGravity(Gravity.CENTER); layout.addView(imageView); layout.addView(textView); toast.setView(layout); toast.setGravity(Gravity.TOP,0,0); toast.setDuration(Toast.LENGTH_LONG); toast.show(); }
Menu
節(jié)省更多的空間,位于右上角的三點
<item android:id="@+id/item_add" android:title="Add" />
然后在Activity里重寫onCreateOptionsMenu()方法
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main,menu); return true; }
getMenuInflater()得到MenuInflater對象,再調(diào)用其方法inflate()給當(dāng)前activity創(chuàng)建菜單
第一個參數(shù)用于指定通過哪一個資源文件夾創(chuàng)建菜單,第二個參數(shù)用于指定我們的菜單將添加到哪一個Menu對象中
返回true,表示顯示菜單,否則菜單無法顯示
重寫onOptionsItemSelected()方法來定義菜單響應(yīng)事件
@Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { switch(item.getItemId()){ case R.id.item_add: Toast.makeText(this,"You click Add",Toast.LENGTH_SHORT).show(); break; case R.id.item_remove: Toast.makeText(this,"You click Remove",Toast.LENGTH_SHORT).show(); break; default: break; } return true; }
到此這篇關(guān)于Android基礎(chǔ)之常用控件屬性介紹的文章就介紹到這了,更多相關(guān)Android常用控件屬性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
以一個著色游戲展開講解Android中區(qū)域圖像填色的方法
這篇文章主要介紹了Android中實現(xiàn)區(qū)域圖像顏色填充的方法,文中以一個著色游戲為例講解了邊界的填充等各種填色操作,需要的朋友可以參考下2016-02-02Android使用Notification實現(xiàn)寬視圖通知欄(二)
這篇文章主要為大家詳細介紹了Android使用Notification實現(xiàn)寬視圖通知欄,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12android FragmentTabhost實現(xiàn)導(dǎo)航分頁
這篇文章主要為大家詳細介紹了android FragmentTabhost實現(xiàn)導(dǎo)航分頁,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08Android 中 android.view.WindowLeaked的解決辦法
這篇文章主要介紹了Android 中 android.view.WindowLeaked的解決辦法的相關(guān)資料,需要的朋友可以參考下2017-05-05Android Framework Application Framework層簡單介紹
這篇文章主要介紹了 Android Framework Application Framework層簡單介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11Android編程基礎(chǔ)之Menu功能菜單設(shè)計實例
這篇文章主要介紹了Android編程基礎(chǔ)之Menu功能菜單,結(jié)合實例形式分析了基本的Menu功能菜單原理、定義與響應(yīng)機制,需要的朋友可以參考下2016-10-10