Android——界面布局
LinearLayout
中兩個相似屬性區分:
<?xml version="1.0" encoding="utf-8"?>
<!--線性布局-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:text="one"
android:layout_gravity="right" />
<!-- 2EditText中的文字在EditText元件中居中顯示-->
<!-- 5讓EditText元件在LinearLayout中居右顯示-->
</LinearLayout>
Button常用方法
activity_demo布局檔案:
<Button
android:layout_width="wrap_content"
android:layout_height="40dip"
android:minWidth="100dip"
android:layout_marginLeft="0dip"
android:layout_marginRight="2dip"
android:layout_marginTop="5dip"
android:layout_marginBottom="10dip"
android:background="@drawable/button"
android:text="@string/login"
android:textColor="#fff"
android:textSize="18sp"
android:id="@+id/login"
android:layout_alignRight="@+id/password"
android:layout_below="@+id/password"
android:onClick="onLoginClick"
/>
<!-- 15屬性的單擊監聽方法為onLoginClick-->
ButtonActivity.java檔案:
package com.example.myapplication;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.view.accessibility.AccessibilityNodeInfo;
public class ButtonActivity {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
// 方法作為 android:onClick="onLoginClick"屬性的Button的單擊方法
public void onLoginClick(View v){
AccessibilityNodeInfo name = null;
if(TextUtils.isEmpty(name.getText().toString())){
name.setError(getString(R.string.app_name));
return;
}
//省略
}
private CharSequence getString(int no_empty_name) {
return null;
}
}
這樣編寫的好處在于可以直接完成按鍵監聽,不必通過調用findViewById(int id)來找到該Button,然後再為其設定單擊監聽器setOnClickListener(OnClickListener)。
strings.xml檔案聲明字元串數組
位置目錄:res——values——strings.xml
<resources>
<string name="app_name">My Application</string>
<string-array name="cities">
<item>北京</item>
<item>上海</item>
<item>成都</item>
</string-array>
</resources>
在代碼中利用Android通過ArrayAdapter.createFromResource()方法來擷取這個字元串數組,再将這個數組資源的Adapter通過setAdapter()方法與Spinner建立關聯,Spinner就能夠使用這個數組來初始化下拉清單的内容了。
水準進度條的xml檔案設定
//僅示例
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal">
style="?android:attr/progressBarStyleLarge"
style="?android:attr/progressBarStyleSmallTitle"
</ProgressBar>
<!-- 3水準進度條-->
<!-- 4較大的進度條-->
<!-- 5标題大小的進度條-->
在玩遊戲時,對于聲音的控制可以采用選項菜單——
建立菜單的步驟:
package com.example.myapplication;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class OptionMenuDemoActivity extends Activity {
// 1覆寫方法onCreateOptionsMenu,通過Menu中的一個add方法建立菜單并且添加菜單項
/**
* 覆寫該方法添加菜單
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//添加菜單項
menu.add(0, 0, 0, "聲音:關");
menu.add(0, 1, 0, "聲音:開");
return super.onCreateOptionsMenu(menu);
}
//2單擊每一個菜單項,可以進行相應的操作,需要覆寫方法onOptionsItemSelected,根據菜單的每個ID進行判斷
/**
* 覆寫方法,對選項菜單進行操作
*/
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){//根據菜單的ID
case 0:
Toast.makeText(OptionMenuDemoActivity.this,"聲音已經關閉!!", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(OptionMenuDemoActivity.this,"聲音已經打開!!", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}
選項菜單的建立很簡單,用的也非常多,它的建立和對其進行操作隻要覆寫兩個方法:
onCreateOptionsMenu
和
onOptionsItemSelected
。
上下文菜單執行個體
- 布局檔案,界面設計的很簡單,暫無
- 步驟如下:
package com.example.myapplication;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import static android.graphics.Color.BLUE;
import static android.graphics.Color.RED;
import static android.graphics.Color.YELLOW;
public class ContextMenuActivity extends Activity {
//覆寫方法onCreateContextMenu,在這個方法中可以添加相應的菜單項
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//建立菜單項
menu.add(0, RED, 0, "紅色");
menu.add(0, BLUE, 0, "藍色");
menu.add(0, YELLOW, 0, "黃色");
}
//覆寫方法onContextItemSelected,對每一個菜單項進行相應的處理,改變字型的顔色
@Override
public boolean onContextItemSelected(MenuItem item) {
TextView textView = null;
switch (item.getItemId()) {
case RED:
textView.setTextColor(RED);
break;
case YELLOW:
textView.setTextColor(YELLOW);
break;
case BLUE:
textView.setTextColor(BLUE);
break;
default:
break;
}
return super.onContextItemSelected(item);
}
// registerForContextMenu(LinearLayout);
}
- 在xml檔案中補充:
<!-- 注冊上下文菜單。如果沒有注冊,單機界面的某一個視圖是沒有反應的,是以上下文菜單與前面的選項菜單不一樣,需要進行注冊,裡面的參數為View-->
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:registerForContextMenu="(LinearLayout)" />
子菜單執行個體
-
布局檔案,界面設計的很簡單,暫無
步驟如下:
package com.example.myapplication;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
public class SubMenuActivity extends Activity {
private static final int ITEM1 = 1;
private static final int ITEM2 = 2;
//覆寫方法onCreateContextMenu,在這個方法中可以添加相應的菜單項
@Override
public boolean onCreateOptionsMenu(Menu menu){
SubMenu file=menu.addSubMenu("檔案");
SubMenu editor=menu.addSubMenu("編輯");
file.add(0,ITEM1,0,"建立");
file.add(0,ITEM2,0,"打開");
return true;
}
//覆寫方法,對子菜單事件進行監聽
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case ITEM1:
setTitle("建立檔案");
break;
case ITEM2:
setTitle("打開檔案");
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
滾動處理執行個體
- 布局檔案,此布局檔案最外面采用ScrollView,嵌套了一個線性布局,按鈕和文本框都線上性布局中。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/LinearLayout01"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="17sp"
android:textColor="#000000"
android:text="ScrollView0" />
<Button
android:id="@id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button0"
/>
</LinearLayout>
</ScrollView>
- 按鈕事件的實作。單擊按鈕增加一個線性布局,這個布局檔案中添加了一個按鈕(Button)和文本框(TextView)
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ScrollViewDemoActivity extends Activity {
//按鈕事件:單擊按鈕增加一個文本框和按鈕
private final View.OnClickListener listener = new View.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(View v) {
TextView textView1 = new TextView(ScrollViewDemoActivity.this);
int index = 0;
//綁定activity_main布局檔案中的布局項,其中R.id.lenearlay_1為布局檔案中設定的id
LinearLayout layout=(LinearLayout) findViewById(R.id.LinearLayout01);
textView1.setText("ScrollView" + index);
textView1.setTextColor(Color.BLACK);
LinearLayout.LayoutParams layoutParams = new LinearLayout.
LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(textView1, layoutParams);//把TextView增加到布局中
//執行個體化一個按鈕
Button btn = new Button(ScrollViewDemoActivity.this);
btn.setText("Button" + index);
//把Button增加到布局中
layout.addView(btn, layoutParams);
}
};
}
示例~
一個首頁面,裡面有九個按鈕,分别指向了9個UI示例,分别是、
SimpleView
、
ImageView
、
ListView
、
ExpandableList
、
Style
、
Theme
、
AlertDialog
、
Menu
Progress
。
SimpleView展示了如何使用EditText、TextView、ImageText、Button等控件;
ImageView展示了如何使用ImageView控件;
ListView展示了如何使用ListView控件;
ExpandableList展示了如何使用ExpandableListView控件;
Style展示了如何建立樣式和使用樣式;
Theme展示了如何建立和使用主題;
AlertDialog展示了如何使用AlertDialog對話框;
Menu展示了菜單控件的建立和使用;
Progress展示了進度條的使用
代碼實作:
所有的Activity元件需要在AndroidManifest.xml檔案中進行注冊