Android03-常見控件的使用

TextView

類似于iOS的UILabel

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#00ff00"
        android:textSize="24sp"
        android:textAlignment="center"
        android:text="This is TextView"/>

Button

  • 顯示一個(gè)button
    <Button
        android:id="@+id/button"
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
  • 給button綁定點(diǎn)擊事件方式:匿名類的方式注冊(cè)監(jiān)聽器
        //1. 通過findViewId()獲得對(duì)應(yīng)的Button
        //2. 添加OnClickListener對(duì)象
        Button btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //處理點(diǎn)擊事件
            }
        });

EditText

類似iOS的UITextField

   <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:hint="我是一個(gè)輸入框"
        android:textColor="#ff00ff"
        android:textColorHint="#0f0fff"
        android:maxLines="2"/>
  • 獲取輸入框的值
        EditText editText = (EditText) findViewById(R.id.editText);
        String string = field.getText().toString();

ImageView

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:id="@+id/image_view"
        android:src="@mipmap/ic_launcher" />
  • 重置imageView的圖片
                //獲得imageView的圖片
                ImageView imgV = (ImageView) findViewById(R.id.image_view);
                imgV.setImageResource(R.drawable.icon_camera);

ProgressBar

用于在界面顯示一個(gè)進(jìn)度條

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"/>
  • 控制進(jìn)度條的相關(guān)屬性
                //控制進(jìn)度條的相關(guān)屬性
                ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
                int progress = progressBar.getProgress();
                progress = progress + 10;
                progressBar.setProgress(progress);
                //控制進(jìn)度條的隱藏與否
                if (progressBar.getVisibility() == View.GONE) {
                    progressBar.setVisibility(View.VISIBLE);
                } else {
                    progressBar.setProgress(View.GONE);
                }

AlertDialog

類似于iOS的警告框

                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("我是警告框");
                dialog.setMessage("我是警告信息");
                dialog.setCancelable(false);
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //處理按鈕點(diǎn)擊事件
                        Toast.makeText(MainActivity.this, "點(diǎn)擊Ok", Toast.LENGTH_LONG).show();
                    }
                });
                dialog.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //處理按鈕點(diǎn)擊事件
                        Toast.makeText(MainActivity.this, "點(diǎn)擊Cancle", Toast.LENGTH_LONG).show();
                    }
                });
                //顯示警告框
                dialog.show();

ProgressDialog

  • 創(chuàng)建一個(gè)ProgressDialog
                //創(chuàng)建一個(gè)ProgressDialog
                ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setTitle("這是一個(gè)ProgressDialog");
                progressDialog.setMessage("Loading...");
                progressDialog.setCancelable(true);
                progressDialog.show();

四種基本布局

  • 布局與控件之間的關(guān)系

LinearLayout

線性布局,所有添加的控件都按照線性排布,可以設(shè)置水平方向或者垂直方向,比較常用android:orientation="horizontal"

  • 比較重要的屬性
  • layout_weight: 可以設(shè)置控件占屏寬的比重,假如控件一設(shè)置該值為2,控件二設(shè)置該值為3,則控件一占2/5,控件二占3/5
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:hint="請(qǐng)輸入賬號(hào)"
        android:id="@+id/edit_second"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="send"
        android:id="@+id/btn1_second"/>
</LinearLayout>

RelativeLayout

相對(duì)布局,可以通過相對(duì)定位的方式,讓控件出現(xiàn)在布局的任何位置。比較常用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button1"
        android:id="@+id/btn1"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/btn1"
        android:layout_above="@id/btn1"
        android:text="Button2"
        android:id="@+id/btn2"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button3"
        android:layout_toRightOf="@id/btn1"
        android:layout_above="@id/btn1"
        android:id="@+id/btn3"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button4"
        android:id="@+id/btn4"
        android:layout_toLeftOf="@id/btn1"
        android:layout_below="@id/btn1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn5"
        android:text="Button5"
        android:layout_toRightOf="@id/btn1"
        android:layout_below="@id/btn1"/>
</RelativeLayout>

FrameLayout

沒有任何定位方式,所有控件都會(huì)擺放在布局的左上角.不常用

TableLayout

使用表格的方式來排列控件,不是很常用。

自定義控件

所有的控件都是直接或間接繼承自View的,所有的布局都是直接或間接繼承自ViewGroup的。

  • View是Android中一種最基本的UI組件,我們使用的所有控件都是在View的基礎(chǔ)上各自特有的功能的;
  • ViewGroup是一種特殊的View,可以包含很多View和ViewGrooup。是一個(gè)放置控件和布局的容器
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容