android 之button 屏幕翻转
来源:互联网 发布:java字符串转换成json 编辑:程序博客网 时间:2024/04/28 06:07
android 之button 屏幕翻转
一、 使用资源文件,对按钮不同状态(按下,获得焦点、弹起)做出改变
二、调用api,实现手机屏幕竖屏和横屏的转换
直接见代码
布局文件main.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="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@drawable/button" />
</LinearLayout>
注意Button > background 中引用资源文件 button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/android_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/android_focused" /> <!-- focused -->
<item android:state_selected="true"
android:drawable="@drawable/android_focused"
></item>
<item android:drawable="@drawable/android_normal" /> <!-- default -->
</selector>
Activity实现类:
package com.time.android.button;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ButtonTestActivity extends Activity {
private Button btn = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ButtonTestActivity.this, "click", Toast.LENGTH_LONG).show();
/*屏幕翻转*/
if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
});
}
}
另外 获得屏幕尺寸方法:
final DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
String res = metrics.widthPixels +":" +metrics.heightPixels;
- android 之button 屏幕翻转
- android 之button 屏幕翻转
- android屏幕翻转
- android 屏幕翻转问题
- android,屏幕翻转问题
- 关于android屏幕翻转
- iOS交互之翻转屏幕
- button 翻转
- Android之翻转静音
- Android中正确自适应屏幕翻转
- Android中正确自适应屏幕翻转
- 【Android】屏幕翻转(screen&nbsp…
- 如何让Android屏幕只能上下翻转
- 使用calabash-android 实现 翻转屏幕
- 屏幕翻转
- Button适配多种android屏幕
- Android 笔记 之 Button
- Android之Button样式
- 英雄联盟 无法登陆
- Sql Server 2008 之 Merge
- 七夕夜、离别,涵情经河岸
- ORA-12514 TNS 监听程序当前无法识别连接描述符中请求的服务
- 如何操作Excel模板,往Excel模板中写入数据
- android 之button 屏幕翻转
- 简易分页类
- About Memory leak
- How can selenium web driver get to know when the new window has opened and then resume its execution
- 转 IT管理者必须了解且规避的12大技术陷阱
- MFC之对话框
- 那年,那场青春繁华梦
- 安装好Windows 8后必做的几件事情,让你的Win8跑的更快更流畅。
- Shell赋值