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文件中进行注册