android竖直和横向,如何在android中为纵向和横向模式定义不同的控件

定义,在这两种资源的文件出现在你的代码 并在onCreate()在所有视图中Activity可以检查方向绑定的意见,你的类的对象。

示例。

在这里我们有包含在这两个文件的ImageView的,并在画像文件有一个TextView和景观它包含Button代替TextView

my_layout.xml在布局2个布局文件-land

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button" />

android:id="@+id/imageView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/abs__ab_bottom_solid_dark_holo" />

my_layout.xml在布局端口文件夹

android:id="@+id/imageView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/abs__ab_bottom_solid_dark_holo" />

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="TextView" />

这里是活动代码

package com.example.stackoverflow;

import android.app.Activity;

import android.content.res.Configuration;

import android.os.Bundle;

import android.view.Display;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.TextView;

public class MyActivity extends Activity {

// landscape views

Button button1;

// protrati views

TextView textView1;

// common views (shared between landscape and protrait mode)

ImageView imageView1;

public MyActivity() {

// TODO Auto-generated constructor stub

}

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.my_layout);

imageView1 =(ImageView) findViewById(R.id.imageView1);//exist inside landscape file and portrait

if(getScreenOrientation() == Configuration.ORIENTATION_PORTRAIT){

textView1 = (TextView)findViewById(R.id.textView1);

}else{

button1 = (Button) findViewById(R.id.button1);

}

//when you want to access any thing that is not shared

//check the orientation

}

@Override

protected void onResume() {

super.onResume();

//let say that we want here to set a text on the textview and it's available only for protrait

if(getScreenOrientation() == Configuration.ORIENTATION_PORTRAIT){

//won't be null :) so we can set the text

textView1.setText("Hello Guys!");

}

}

// http://stackoverflow.com/a/6236110/671676

public int getScreenOrientation() {

Display getOrient = getWindowManager().getDefaultDisplay();

int orientation = Configuration.ORIENTATION_UNDEFINED;

if (getOrient.getWidth() == getOrient.getHeight()) {

orientation = Configuration.ORIENTATION_SQUARE;

} else {

if (getOrient.getWidth() < getOrient.getHeight()) {

orientation = Configuration.ORIENTATION_PORTRAIT;

} else {

orientation = Configuration.ORIENTATION_LANDSCAPE;

}

}

return orientation;

}

}

问什么,你不明白。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值