常用控件的使用方法-2

4、ImageView:用於在界面上展示圖片,

<ImageView 
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" 
        />

android:src 表示指定圖片的名稱,
android:layout_width 和 android:layout_height 指定為wrap_content(隨內容)
在替换图片的时候需注意:
1. 文件不能只包含数字
2. 不能包含一些奇怪的符号 好比 <>-][ {} 各种奇葩符号
3. 确保文件是否存在
4. 在用代码修改图片路径的时候 不能包含 后缀名 比如 web.jpg 直接写web 就OK了
否則會报错 Failed to convert @drawable/ic_launcherweb– into a drawable Exception details are logged in Window > Show View > Error Log

按鈕監聽器中通過ImageView的 setImageResource()方法來實現點擊按鈕時替換圖片

imageView.setImageResource(R.drawable.test);

5、ProgressBar 設定進度條

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        style="?android:attr/progressBarStyleHorizontal"
        />

style 風格有很多種:

  1. style=”?android:attr/progressBarStyleHorizontal”:長方形進度條
    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="30sp" 
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
        android:progress="30"
        android:secondaryProgress="60"
        />

android:max:最大進度值
android:progress : 初始進度值
android:secondaryProgress:当前初始化第2进度值

int progress = progressBar.getProgress();
            progress = progress+10;
            progressBar.setProgress(progress);

onCreate()方法中通過getProgress()獲取當前進度值,通過setProgress()設定進度值
2. style=”?android:attr/progressBarStyleLarge” :超大圆形

<ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        style="?android:attr/progressBarStyleLarge"
        />

android:visibility可設定控件的可見屬性,可見屬性包含:visibility(可見,且默認);invisibility(不可見,但仍會佔用屏幕的位置和大小);gone(不可見,且不占用任何屏幕空間)
onCreate()方法中通過getVisibility()獲取當前進度條的狀態,通過setVistibility()設定進度條的狀態,可以傳入的值有View.VISIBLE;View.INVISIBLE;View.GONE。
以下代碼實現 執行按鈕時進度條消失,再執行按鈕時,進度條可見。

if(progressBar.getVisibility() ==View.GONE)
    progressBar.setVisibility(View.VISIBLE);
else 
    progressBar.setVisibility(View.GONE);
  1. style=”?android:attr/progressBarStyleSmallTitle”:標題小進度條
    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        style="?android:attr/progressBarStyleSmallTitle"
        />

在onCreate()方法中設定顯示標題小進度條

 //设置标题不确定性进度条风格 
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
 //显示标题不确定性进度条 
setProgressBarIndeterminateVisibility(true); 
 //关闭标题不确定性进度条 
 //setProgressBarIndeterminateVisibility(false); 

6.ProgressDialog : 彈出進度條對話框

ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("This is a ProgressDialog");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.show();

通過setTitle設定彈出框的標題;
通過setMessage 設定彈出框的內容
通過setCancelable(true)設定彈出框是否可見,參數為true表示可見,false表示不可見
通過show()將ProgressDialog 顯示出來

7、AlertDialog:彈出框
在當前界面彈出一個對話框,這個對話框是置頂與所有界面元素之上的,能夠屏蔽掉其他控件的交互能力,因此一般AlertDialog都是用於提示一些非常重要的內容或警告信息。

AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
            alertDialog.setTitle("This is a AlertDialog");
            alertDialog.setMessage("Something important...");
            alertDialog.setCancelable(true);
            alertDialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {             
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                }
            });
            alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                }
            });
            alertDialog.show();

通過 AlertDialog.Builder 創建一個AlertDialog 實例;
通過setTitle設定彈出框的標題;
通過setMessage 設定彈出框的內容
通過setCancelable(true)設定彈出框是否可見,參數為true表示可見,false表示不可見
通過setPositiveButton()為該對話框設定確定按鈕的點擊事件;
通過setNegativeButton()為該對話框設定取消按鈕的點擊事件;
通過show()將ProgressDialog 顯示出來。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值