android常见布局补充
2015-08-05 00:00
441 查看
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true" //相对父窗体的下面
android:orientation="vertical"
android:gravity="center_horizontal" //设置其子构件居中显示
tools:context=".MainActivity" >
</LinearLayout>
<ToggleButton //开关按钮
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1" on和off
android:layout_below="@+id/textView1"
android:layout_marginTop="61dp"
android:text="ToggleButton" />
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" //在父窗体的中间显示图片
android:src="@drawable/ic_launcher" />
<TextView
android:inputType="textPassword" //密码***
android:layout_marginRight="5dip" //右间距5dip
android:layout_toRightOf="@id/iv_image" //位置在iv_image的右边
android:textSize="16sp" //字体大小16sp
android:textColor="#000000" //字体颜色
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="新闻标题" //默认显示信息
android:singleLine="true" //只显示一行
android:id="@+id/tv_title"
/>
<TextView
android:layout_marginRight="5dip"
android:layout_below="@id/tv_title" //位置在tv_title的下边
android:layout_toRightOf="@id/iv_image" //在iv_image的右边
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="新闻描述"
android:maxLines="3" //最多显示三行,要配合text使用
android:textSize="13sp"
android:id="@+id/tv_desc"
/>
<TextView
android:layout_marginRight="5dip" //右间距5dip
android:layout_alignRight="@id/tv_desc" //与tv_desc右对齐
android:layout_below="@id/tv_desc" //在tv_desc下方
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跟帖"
android:textSize="10sp"
android:id="@+id/tv_type"
/>
<ScrollView //为TextView添加滚动条,可滚动显示多条
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView>
<CheckBox //复选框
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
<SeekBar //调节进度条
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
/>
// 2、checkbox获取保存的值
boolean cbIsChecked = sp.getBoolean("ischecked", false);
// Value to return if this preference does not exist.参数2:如果不存在该preference的默认返回值。
cBox.setChecked(cbIsChecked);
// 3、seekbar获取保存的值
int sbProgress = sp.getInt("seekBarProgress", 50);// 如果没有,设置为50
seekBar.setProgress(sbProgress);
seekBar.setOnSeekBarChangeListener
public void onProgressChanged(SeekBar seekBar, int progress, // progress表示0~seekbar中max的值之间
boolean fromUser) {
Editor sbEditor = sp.edit();
sbEditor.putInt("seekBarProgress", progress);
sbEditor.commit();
}
设置透明界面:-----------------在项目清单文件中
<activity
android:name="com.itheima.tranlucent.MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent" > //将界面设置为透明
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//点击按钮 开启照相机
public void click1(View v){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//保存一张图片
file = new File(Environment.getExternalStorageDirectory().getPath(),"photo.png");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
//开启一个带返回值的Activity 1:请求码
startActivityForResult(intent, 1);
}
//点击按钮 开始录像
public void click2(View v){
//开始录像
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
//保存一张图片
file = new File(Environment.getExternalStorageDirectory().getPath(),"photo.3gp");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
//开启一个带返回值的Activity 1:请求码
startActivityForResult(intent, 2);
}
menu菜单
getMenuInflater() :只能填充menu文件夹下的布局
LayouInflater() :填充layout文件夹下的布局
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.main, menu);
menu.add(0, 1, 0, "前进");
menu.add(0, 2, 0, "后退");
return true;
}
// 需要知道具体点击的是哪个menu 的item
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
System.out.println("前进");
break;
case 2:
System.out.println("后退");
break;
}
return super.onOptionsItemSelected(item);
}
自动补全:
<AutoCompleteTextView
android:id="@+id/actv"
android:layout_width="match_parent"
android:completionThreshold="1"
android:layout_height="wrap_content" />
private static final String[] COUNTRIES = new String[] {
"laofang", "laozhang", "laobi", "laoli", "xiaoming","xiaohua"
};
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.actv);
//(2)创建数组适配器
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
//(3)设置内容
actv.setAdapter(adapter);
<!-- 把当前activity注册到系统中,作为一个浏览器 -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:scheme="about" />
<data android:scheme="javascript" />
</intent-filter>
固定界面方向和禁用生命周期方法
固定界面的方向:在activity标签的screenOrientation中设置为landscape 横屏,portrait竖屏,sensor传感器类型;
android:screenOrientation="portrait" //一直竖屏
禁用activity生命周期的方法:在activity标签中设置configChanges属性:orientation|screenSize|keyboardHidden;
android:configChanges="orientation|screenSize|keyboardHidden" //禁用activity生命周期
启动模式:决定activity是否被创建,打开一个界面;
android:launchMode="standard"
android:launchMode="singletop"
singletop单一顶部模式:当打开这个activity时系统会判断当前位于栈顶的任务是不是这个activity的任务,如果是,就不创建activity实例对象了,让你使用已经位于栈顶的任务.浏览器的书签;
android:launchMode="singletask"
这个activity在任务栈中只有一个实例存在;在打开界面时,系统会检查任务栈中是否已经存在这个activity对应的任务,如果存在就会把位于这个任务上面的其他任务都清除掉(把界面关闭掉),然后使用已经存在任务对应的activity界面;浏览器;
android:launchMode="singleInstance"
单例模式:在打开这个界面时,系统会提供一个单独的任务栈给它,里面只有这一个任务;电话拨号器;
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true" //相对父窗体的下面
android:orientation="vertical"
android:gravity="center_horizontal" //设置其子构件居中显示
tools:context=".MainActivity" >
</LinearLayout>
<ToggleButton //开关按钮
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1" on和off
android:layout_below="@+id/textView1"
android:layout_marginTop="61dp"
android:text="ToggleButton" />
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" //在父窗体的中间显示图片
android:src="@drawable/ic_launcher" />
<TextView
android:inputType="textPassword" //密码***
android:layout_marginRight="5dip" //右间距5dip
android:layout_toRightOf="@id/iv_image" //位置在iv_image的右边
android:textSize="16sp" //字体大小16sp
android:textColor="#000000" //字体颜色
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="新闻标题" //默认显示信息
android:singleLine="true" //只显示一行
android:id="@+id/tv_title"
/>
<TextView
android:layout_marginRight="5dip"
android:layout_below="@id/tv_title" //位置在tv_title的下边
android:layout_toRightOf="@id/iv_image" //在iv_image的右边
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="新闻描述"
android:maxLines="3" //最多显示三行,要配合text使用
android:textSize="13sp"
android:id="@+id/tv_desc"
/>
<TextView
android:layout_marginRight="5dip" //右间距5dip
android:layout_alignRight="@id/tv_desc" //与tv_desc右对齐
android:layout_below="@id/tv_desc" //在tv_desc下方
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跟帖"
android:textSize="10sp"
android:id="@+id/tv_type"
/>
<ScrollView //为TextView添加滚动条,可滚动显示多条
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView>
<CheckBox //复选框
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
<SeekBar //调节进度条
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
/>
// 2、checkbox获取保存的值
boolean cbIsChecked = sp.getBoolean("ischecked", false);
// Value to return if this preference does not exist.参数2:如果不存在该preference的默认返回值。
cBox.setChecked(cbIsChecked);
// 3、seekbar获取保存的值
int sbProgress = sp.getInt("seekBarProgress", 50);// 如果没有,设置为50
seekBar.setProgress(sbProgress);
seekBar.setOnSeekBarChangeListener
public void onProgressChanged(SeekBar seekBar, int progress, // progress表示0~seekbar中max的值之间
boolean fromUser) {
Editor sbEditor = sp.edit();
sbEditor.putInt("seekBarProgress", progress);
sbEditor.commit();
}
设置透明界面:-----------------在项目清单文件中
<activity
android:name="com.itheima.tranlucent.MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent" > //将界面设置为透明
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//点击按钮 开启照相机
public void click1(View v){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//保存一张图片
file = new File(Environment.getExternalStorageDirectory().getPath(),"photo.png");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
//开启一个带返回值的Activity 1:请求码
startActivityForResult(intent, 1);
}
//点击按钮 开始录像
public void click2(View v){
//开始录像
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
//保存一张图片
file = new File(Environment.getExternalStorageDirectory().getPath(),"photo.3gp");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
//开启一个带返回值的Activity 1:请求码
startActivityForResult(intent, 2);
}
menu菜单
getMenuInflater() :只能填充menu文件夹下的布局
LayouInflater() :填充layout文件夹下的布局
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.main, menu);
menu.add(0, 1, 0, "前进");
menu.add(0, 2, 0, "后退");
return true;
}
// 需要知道具体点击的是哪个menu 的item
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
System.out.println("前进");
break;
case 2:
System.out.println("后退");
break;
}
return super.onOptionsItemSelected(item);
}
自动补全:
<AutoCompleteTextView
android:id="@+id/actv"
android:layout_width="match_parent"
android:completionThreshold="1"
android:layout_height="wrap_content" />
private static final String[] COUNTRIES = new String[] {
"laofang", "laozhang", "laobi", "laoli", "xiaoming","xiaohua"
};
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.actv);
//(2)创建数组适配器
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
//(3)设置内容
actv.setAdapter(adapter);
<!-- 把当前activity注册到系统中,作为一个浏览器 -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:scheme="about" />
<data android:scheme="javascript" />
</intent-filter>
固定界面方向和禁用生命周期方法
固定界面的方向:在activity标签的screenOrientation中设置为landscape 横屏,portrait竖屏,sensor传感器类型;
android:screenOrientation="portrait" //一直竖屏
禁用activity生命周期的方法:在activity标签中设置configChanges属性:orientation|screenSize|keyboardHidden;
android:configChanges="orientation|screenSize|keyboardHidden" //禁用activity生命周期
启动模式:决定activity是否被创建,打开一个界面;
android:launchMode="standard"
android:launchMode="singletop"
singletop单一顶部模式:当打开这个activity时系统会判断当前位于栈顶的任务是不是这个activity的任务,如果是,就不创建activity实例对象了,让你使用已经位于栈顶的任务.浏览器的书签;
android:launchMode="singletask"
这个activity在任务栈中只有一个实例存在;在打开界面时,系统会检查任务栈中是否已经存在这个activity对应的任务,如果存在就会把位于这个任务上面的其他任务都清除掉(把界面关闭掉),然后使用已经存在任务对应的activity界面;浏览器;
android:launchMode="singleInstance"
单例模式:在打开这个界面时,系统会提供一个单独的任务栈给它,里面只有这一个任务;电话拨号器;
相关文章推荐
- android 之 SurfaceView使用(桌面弹球)
- Android各个发行版本对应使用的SQLite版本
- Android APK安装常见错误列表
- Android 退出程序时关闭所有的activity的几种方法
- 【Android适配器系列】BaseAdapter学习笔记
- Android ADB调试
- Android 开源项目
- 解决错误:AndroidManifest.xml file missing
- Android Studio升级到最新v1.3版后配置GitHub提示Gradle版本不支持的问题
- Android远程服务四:远程服务service端和client端的线程关系
- Android Framework_Android系统启动过程
- Android 实现点击两个按钮,原地切换界面
- android新版本下获取屏幕宽度和高度的方法
- 在使用Android-PullToRefresh前遇到的问题
- Android Studio 使用遇到的各种问题及解决方案汇总
- android 何时使用Service 何时使用Thread
- android studio "Cannot launch SDK manager ...."
- Android权限详解
- Android 通过UDP广播建立Socket
- Android(Lollipop/5.0) Material Design(二) 入门指南