《第一行代碼--Android》讀書筆記之UI篇

比較難掌握的UI控件

ProgressBar

  • XML屬性:
    android:progressBarStyle:默認進度條樣式
    android:progressBarStyleHorizontal:水平樣式
    android:progress:初始化進度條的值
    android:max:最大進度值
    style=”?android:attr/progressBarStyleHorizontal”:設置風格爲水平長形
  • 重要方法:
    getMax():返回這個進度條的範圍的上限
    getProgress():返回進度
    getSecondaryProgress():返回次要進度
    incrementProgressBy(int diff):在原來進度的基礎上指定增加的進度,可傳負數進去
    setVisibility(int v):設置該進度條是否可視,傳入參數,View.VISIBLE、View.INVISIBLE、View.GONE三種值

AlertDialog

AlertDialog可以在當前的界面彈出一個對話框,屏蔽掉其他控件的交互能力。
創建過程:
1、創建一個AlertDialog.Builder實例
AlertDialog.Builder dialog =new AlertDialog.Builder(Context context);
2、配置Builder的屬性,如:
dialog.setTitle();
dialog.setMessage();
dialog.setCancelable();
還可以設置響應按鈕點擊事件的代碼:

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //設置按鈕點擊事件
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

3、顯示對話框
dialog.show();

自定義控件

引入佈局

1、創建自定義控件的佈局,custom_view.xml
2、在需要的佈局文件引入佈局,< include layout=”@layout/custom_view”/>

創建自定義控件

1、自定義一個繼承自LinerLayout的佈局類CustomLayout, 重寫LinearLayout的帶兩個參數的構造函數,通過LayoutInflater.from(context).inflate(@LayoutRes int resource, @Nullable ViewGroup root)構建出一個LayoutInflater對象實現自定義佈局的動態加載,並響應佈局內的控件點擊響應。

public class CustomLayout extends LinearLayout {
    public CustomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.custom_view, this);
        ImageButton button_back=(ImageButton)findViewById(R.id.Button_1);
        ImageButton button_edit=(ImageButton)findViewById(R.id.Button_2);
        button_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity)getContext()).finish();
            }
        });
        button_edit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(),"yout click the editButton.",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

2、在有需要的佈局文件引入自定義控件,要指定完整類名,包名不能省略

<com.example.users.uilayouttest.CustomLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </com.example.users.uilayouttest.CustomLayout>

ListView

這裏寫圖片描述

優化方法:
a)、每一個子項被滾動到屏幕內都會調用getView()方法,爲了優化快速滾動的性能,不必每次都要加載一次佈局,因爲getView()方法中有一個convertView參數,用於將之前加載好的佈局進行緩存以便重用。
b)、不必每次都要用findViewById()獲取子項佈局控件的實例,將子項佈局中的控件各個字段封裝成一個類,利用view.setTag()和view.getTag()來存儲和獲取子項佈局中的控件。

爲了響應listview的點擊事件,需要在listview中註冊監聽器,回調onItemClick()方法,通過其中的position參數判斷用戶點擊的是哪個子項。

msgListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String message = list.get(position).getMessage();
                Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });

優化後的自定義adaptor類

public class FruitAdapter extends ArrayAdapter<Fruit> {
    class ViewHolder {
        TextView fruit_name;
        ImageView fruit_image;
    }
    private int itemLayoutId;
    public FruitAdapter(Context context, int resource, List<Fruit> objects) {
        super(context, resource, objects);
        itemLayoutId=resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Fruit item=getItem(position);
        View view;
        ViewHolder viewHolder;
        if(convertView==null){
            view=LayoutInflater.from(getContext()).inflate(itemLayoutId,null);
            viewHolder=new ViewHolder();
            viewHolder.fruit_image=(ImageView)view.findViewById(R.id.Fruit_Image);
            viewHolder.fruit_name=(TextView)view.findViewById(R.id.Fruit_Name);
            view.setTag(viewHolder);
        }else {
            view=convertView;
            viewHolder=(ViewHolder)view.getTag();
        }
        viewHolder.fruit_name.setText(item.getName());
        viewHolder.fruit_image.setImageResource(item.getImageId());
//        View view= LayoutInflater.from(getContext()).inflate(itemLayoutId, null);
//        TextView nameId=(TextView)view.findViewById(R.id.Fruit_Name);
//        ImageView imageId=(ImageView)view.findViewById(R.id.Fruit_Image);
//        nameId.setText(item.getName());
//        imageId.setImageResource(item.getImageId());
        return view;
    }
}

四大基本佈局


  • LinearLayout
    線性佈局,關鍵屬性:
    • android:orientation=”vertical/horizontal” 用於指定排列方向
    • android:layout_gravity=”?” 用於指定空間對齊方式,而android:layout指定的是文字在控件的對齊方式
    • android:layout_weight=”?” 用於使用比例指定空間的大小
  • RelativeLayout
    相對佈局,通過相對定位的方式讓空間出現在佈局的任何位置。
    關鍵屬性:
    • 第一類:屬性值爲true或false
        android:layout_centerHrizontal 水平居中
        android:layout_centerVertical 垂直居中
        android:layout_centerInparent 相對於父元素完全居中
        android:layout_alignParentBottom 貼緊父元素的下邊緣
        android:layout_alignParentLeft 貼緊父元素的左邊緣
        android:layout_alignParentRight 貼緊父元素的右邊緣
        android:layout_alignParentTop 貼緊父元素的上邊緣
        android:layout_alignWithParentIfMissing 如果對應的兄弟元素找不到的話就以父元素做參照物
    • 第二類:屬性值必須爲id的引用名“@id/id-name”
        android:layout_below 在某元素的下方
        android:layout_above 在某元素的的上方
        android:layout_toLeftOf 在某元素的左邊
        android:layout_toRightOf 在某元素的右邊
        android:layout_alignTop 本元素的上邊緣和某元素的的上邊緣對齊
        android:layout_alignLeft 本元素的左邊緣和某元素的的左邊緣對齊
        android:layout_alignBottom 本元素的下邊緣和某元素的的下邊緣對齊
        android:layout_alignRight 本元素的右邊緣和某元素的的右邊緣對齊
    • 第三類:屬性值爲具體的像素值,如30dip,40px
        android:layout_marginBottom 離某元素底邊緣的距離
        android:layout_marginLeft 離某元素左邊緣的距離
        android:layout_marginRight 離某元素右邊緣的距離
        android:layout_marginTop 離某元素上邊緣的距離
  • FrameLayout
    所有的控件都擺放到佈局的左上角,比較另類。
  • TableLayout
    使用表格的方式來排列控件,具體請看這裏還有這裏

參考的資料和文獻:
一網打盡Android UI 控件系列集錦:http://mobile.51cto.com/abased-405966.htm
RelativeLayout佈局屬性詳解:http://blog.sina.com.cn/s/blog_6ca887bb01011wwr.html
Android ProgressBar 相關設置講解:http://blog.csdn.net/brokge/article/details/8532662
Android多種進度條使用詳解:http://www.codeceo.com/article/android-progressbars.html
多式樣ProgressBar:http://www.apkbus.com/android-735-1-1.html

發佈了32 篇原創文章 · 獲贊 45 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章