Android UI(二)
相對布局(RelativeLayout)
特點
- 需要找基準點,來確定控件位置
常用屬性
相對於某控件外側對齊(上對下,下對上)
左邊:android:layout_toLeftOf
右邊:android:layout_toRightOf
上方:android:layout_above
下方:android:layout_below
相對於某控件邊緣對齊(上對上)
對齊上邊界:android:layout_alignTop
對齊下邊界:android:layout_alignBottom
對齊左邊界:android:layout_alignLeft
對齊右邊界:android:layout_alignRight
相對於父控件邊緣對齊(上對上)
左對齊:android:layout_alighParentLeft
右對齊:android:layout_alighParentRight
頂端對齊:android:layout_alighParentTop
底部對齊:android:layout_alighParentBottom
水平居中:android:layout_centerHorizontal
垂直居中:android:layout_centerVertical
中央位置:android:layout_centerInParent
相對於某控件的外邊距
android:layout_margin:
android:layout_marginLeft: 指定控件的左邊的外部留出一定的邊距
android:layout_marginTop: 指定控件的上邊的外部留出一定的邊距
android:layout_marginRight: 指定控件的右邊的外部留出一定的邊距
android:layout_marginBottom: 指定控件的下邊的外部留出一定的邊距內邊距
android:padding :指定控件的四周的內部留出一定的邊距
android:paddingLeft: 指定控件的左邊的內部留出一定的邊距
android:paddingTop: 指定控件的上邊的內部留出一定的邊距
android:paddingBottom: 指定控件的下邊的內部留出一定的邊距補充
- android:hint可以設置文本框的提示信息
- android:gravity
android:gravity屬性是對該view 內容的限定.比如一個button 上面的text. 你可以設置該text 在view的靠左,靠右等位置.以button為例,android:gravity="right"則button上面的文字靠右
- android:layout_gravity
android:layout_gravity是用來設置該view相對與起父view 的位置.比如一個button 在linearlayout裏,你想把該button放在靠左、靠右等位置就可以通過該屬性設置.以button為例,android:layout_gravity="right"則button靠右
參考自
常用控件
單選按鈕(RandioButton)
按鈕之間實現互斥
把它們包裹在RadioGroup控件中
RadioGroup可以設置按鈕的排列方式
xml <RadioGroup android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/tv1"> <!--設置orientation屬性可以設置裏面按鈕的布局方式: horizontal(水平排列),vertical(垂直排列)--> <RadioButton android:button="@drawable/rg_selector" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="等額本息"/> <!--RadioButton是:CompoundButton的子類,設置selector選擇器得通過button 屬性 而不是background屬性,--> <!-- 可以通過設置android:checked="true"來使某個按鈕默認被選中--> <RadioButton android:button="@drawable/rg_selector" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="等額本金"/> </RadioGroup>
事件處理
通常是對RadioGroup來進行事件綁定
RadioGroup rg=findViewById(R.id.rg); // 獲取RadioGroup,對他綁定setOnCheckedChangeListener事件 rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override // i代表的是選中的那個按鈕的id值 //也可以通過group.getCheckedRadioButtonId()來判斷 public void onCheckedChanged(RadioGroup radioGroup, int i) { if(i==R.id.rb1) textview.setText("你選的是男"); else if(i==R.id.rb2) textview.setText("你選的是女"); } });
復選按鈕(CheckBox)
特點
同樣為CompoundButton的子類,所以和RadioButton差不多,不他需要出現互斥,所以不需要把它們放到一個組裏
設置監聽
setOnCheckedChangeListener方法需要實現OnCheckedChangeListener接口其他
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String desc = String.format("您%s了這個CheckBox", isChecked?"勾選":"取消勾選");
buttonView.setText(desc);
}
//第一個參數:勾選改變的復選按鈕對象 第二個參數:勾選狀態
//也可以單獨對某個復選框使用isCheck()方法判斷他是否被選中
Swith開關
<Switch
android:id="@+id/switchtt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON"
android:thumb="@drawable/switch_thumb_selector"
android:track="@drawable/switch_track_selector" />
<!--android:thumb屬性代表的是滑塊的樣式。 android:track代表的是滑到的樣式-->
事件監聽
// switch部分 Switch sw=findViewById(R.id.switch1); boolean b= sw.isChecked(); final TextView tvv=findViewById(R.id.textviewt); sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if(b==true) tvv.setText("你打開啦這個開關"); else tvv.setText("你關閉啦這個開關"); } });
Spinner下拉列表
使用方式
<!--設置他的spinnerMode屬性來實現下拉列表的展示方式 android:spinnerMode="dropdown"在下拉框的正下方展示 android:spinnerMode="dialog"在頁面中部以對話框形式展示 --> <Spinner android:id="@+id/s1" android:spinnerMode="dialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/textv1" style="@style/spinner" ></Spinner>
java類
// 創建一個ArrayAdapter數組適配器傳入的參數第一個Context對象一般為this , // 第二個布局文件的id,要顯示的數據的String數組形式 ArrayAdapter aa=new ArrayAdapter(this,R.layout.layout3,startArry); // 給Spinner設置適配器 s1.setAdapter(aa);
布局文件
<?xml version="1.0" encoding="utf-8"?> <!--只能包含一個TextView--> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" style="@style/spinner" android:singleLine="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:textAlignment="inherit" android:textColor="@color/colorAccent" />
數組適配器的用法
一、簡單的。
這樣的列表的每一行都只有一行文字。
// 當然listview 也可以是在layout裏寫好,然後findViewById()獲取出來,這樣的話後面就不需setContentView(listview); ListView listview = new ListView(this); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1); adapter.add("string1"); adapter.add("haha"); adapter.add("heihei"); listview.setAdapter(adapter); setContentView(listview);
上面代碼中,android.R.layout.simple_expandable_list_item_1是android裏已提供的樣式,我們也可換成自己的xml。但是需要註意的是這個xml文件僅能有一個textview。連Layout也不能有。否則會報錯:ArrayAdapter requires the resource ID to be a TextView
如layout下有online_user_list_item.xml,它的內容如下:
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/online_user_list_item_textview" > </TextView>
則android.R.layout.simple_expandable_list_item_1換成R.layout.online_user_list_item。
如果我們想要使用更復雜一點的layout,而不僅是只有一個textview,那就要用下面這種。
二、樣式豐富但內容簡單的。
layout下的online_user_list_item.xml內容如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/online_user_list_item_textview" android:text="TextView"></TextView> <Button android:text="button" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> </LinearLayout>
裏面含有的textview是我們想要展示內容的地方。那麽構建ArrayAdapter時,應該這樣寫:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.online_user_list_item, R.id.online_user_list_item_textview);
如果我們需要展示的內容是一僅一個textview承載不了的,還需要其它組件,怎麽辦?我們可以自定義。
三、內容豐富的(自定義ArrayAdapter)。
這就需要寫一個類繼承自ArrayAdapter並且重寫getView方法。上代碼:
public class UserListAdapter extends ArrayAdapter<User> { private int resourceId; public UserListAdapter(Context context, int textViewResourceId, List<User> objects) { super(context, textViewResourceId, objects); this.resourceId = textViewResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent){ User user = getItem(position); LinearLayout userListItem = new LinearLayout(getContext()); String inflater = Context.LAYOUT_INFLATER_SERVICE; LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater); vi.inflate(resourceId, userListItem, true); TextView tvUsername = (TextView)userListItem.findViewById(R.id.tv_user_list_username); TextView tvAskedNum = (TextView)userListItem.findViewById(R.id.tv_user_list_askednum); TextView tvLastMsg = (TextView)userListItem.findViewById(R.id.tv_user_list_lastmsg); tvUsername.setText(user.getUsername()); tvAskedNum.setText(String.valueOf(user.getAskedNum())); tvLastMsg.setText(user.getLastMsg()); return userListItem; } }
activity裏就這樣寫
List<User> users = new ArrayList<User>(); User user = new User(); user.setAskedNum(8); user.setLastMsg("hello"); user.setUsername("pxx"); users.add(user); users.add(user); users.add(user); UserListAdapter adapter = new UserListAdapter(this,R.layout.online_user_list_item,users); listview.setAdapter(adapter);
參考自
Android UI(二)