网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

横竖屏切换显示不同布局-创新互联

在项目开发中,

创新互联专注于高平企业网站建设,响应式网站建设,商城网站定制开发。高平网站建设公司,为高平等地区提供建站服务。全流程按需开发,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

关于android手机横竖屏切换时显示不同的界面,在这里我定义了两个xml布局文件

landscape_screen.xml,portrait_screen.xml

根据屏幕的旋转切换不同的布局文件

重写onConfigurationChanged方法,对其进行监听并判断当前的屏幕状态,根据其状态显示对应的布局文件

当然在manifest.xml中对应的activity中要加上

android:configChanges="keyboardHidden|orientation|screenSize">

贴上代码说话

public classScreenActivity extends Activity implements OnClickListener{

private Button btn;

@Override

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//默认为竖屏

setupViewInPortraitLayout();

}

//竖屏界面

private voidsetupViewInPortraitLayout(){

setContentView(R.layout.portrait_screen);

btn = (Button)findViewById(R.id.button1_portrait);

btn.setOnClickListener(this);

}

//横屏界面

private voidsetupViewInLandscapeLayout(){

setContentView(R.layout.landscape_screen);

btn = (Button)findViewById(R.id.button1_landscape);

btn.setOnClickListener(this);

}

@Override

public voidonConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);

Toast.makeText(ScreenActivity.this,"onConfigurationChanged", Toast.LENGTH_LONG).show();

Configuration cfg =getResources().getConfiguration();

if (cfg.orientation ==Configuration.ORIENTATION_LANDSCAPE) {

setupViewInLandscapeLayout();

} else if(cfg.orientation == Configuration.ORIENTATION_PORTRAIT) {

setupViewInPortraitLayout();

}

}

@Override

public void onClick(Viewarg0) {

// TODO Auto-generatedmethod stub

if(arg0.equals(btn)){

Toast.makeText(ScreenActivity.this,"Click", Toast.LENGTH_LONG).show();

}

}

}

//landscape_screen.xml

"1.0"encoding="utf-8"?>

"http://schemas.android.com/apk/res/android"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   android:orientation="vertical">

   

       android:id="@+id/button1_landscape"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:text="LandScape"/>

   

       android:id="@+id/textView1"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:gravity="center"

       android:text="横屏界面"

       android:textAppearance="?android:attr/textAppearanceLarge"/>

//portrait_screen.xml

"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:paddingBottom="@dimen/activity_vertical_margin"

   android:paddingLeft="@dimen/activity_horizontal_margin"

   android:paddingRight="@dimen/activity_horizontal_margin"

   android:paddingTop="@dimen/activity_vertical_margin"

   tools:context=".ScreenActivity">

   

       android:id="@+id/button1_portrait"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:layout_alignParentLeft="true"

       android:layout_alignParentRight="true"

       android:layout_centerVertical="true"

       android:text="Portrait"/>

   

       android:id="@+id/textView1"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:layout_alignParentLeft="true"

       android:layout_below="@+id/button1_portrait"

       android:gravity="center"

       android:text="竖屏界面"

       android:textAppearance="?android:attr/textAppearanceLarge"/>

//manifest.xml

"1.0"encoding="utf-8"?>

"http://schemas.android.com/apk/res/android"

   package="com.example.testscreen"

   android:versionCode="1"

   android:versionName="1.0">

   

       android:minSdkVersion="8"

       android:targetSdkVersion="18"/>

   

       android:allowBackup="true"

       android:icon="@drawable/ic_launcher"

       android:label="@string/app_name"

       android:theme="@style/AppTheme"

       >

       

           android:name="com.example.testscreen.ScreenActivity"

           android:label="@string/app_name"

           android:configChanges="keyboardHidden|orientation|screenSize">

           

               "android.intent.action.MAIN"/>

               "android.intent.category.LAUNCHER"/>

           

       

   

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享文章:横竖屏切换显示不同布局-创新互联
文章URL:http://bjjierui.cn/article/jisdg.html

其他资讯