我的应用程序在纵向模式,TextSwitcher,自定义视图和自定义键盘中有三个视图。 在横向模式下,我需要删除TextSwitcher并在右侧添加一个ListView。 我是新来的android,我可以定义两个布局,即xxx和xxx-land,我的问题是如何实现代码的两个分支,一个用于控制风景,一个用于肖像。如何在android中为纵向和横向模式定义不同的控件
0
A
回答
1
定义,在这两种资源的文件出现在你的代码 并在onCreate()
在所有视图中Activity
可以检查方向绑定的意见,你的类的对象。
示例。
在这里我们有包含在这两个文件的ImageView的,并在画像文件有一个TextView
和景观它包含Button
代替TextView
my_layout.xml在布局2个布局文件-land
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abs__ab_bottom_solid_dark_holo" />
</LinearLayout>
my_layout.xml在布局端口文件夹
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abs__ab_bottom_solid_dark_holo" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
这里是活动代码
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;
}
}
问什么,你不明白。
3
设置一个标志,基于当前方向
getResources().getConfiguration().orientation
和访问textswitcher或ListView检查标志...并根据标志采取行动,例如在纵向模式下进行更改Textswitcher之前,但在景观不要因为这将是空...
相关问题
- 1. 如何为纵向和横向定义不同的布局?
- 2. 为纵向和横向定义了不同的布局?
- 3. 方向纵向和横向模式
- 4. UIScrollView在纵向模式和横向模式下的行为不同
- 5. UISplitViewController中的ActionSheet在纵向模式中的行为与横向模式不同
- 6. Android:如何处理纵向反转纵向和横向反转横向事件
- 7. Android定制主题:针对横向和纵向模式的不同主题
- 8. 如何在android中控制webview横向和纵向?
- 9. iPad - 用于横向和纵向模式的不同设计
- 10. Nexus 7 - 用于纵向和横向模式的不同布局
- 11. IOS纵向和横向模式
- 12. 设置不同的初始规模为横向和纵向
- 13. Android小部件不同的纵向和横向
- 14. UISplitViewController出现在横向模式纵向
- 15. Android相机在纵向和横向模式下保存图像
- 16. 如何在android中以编程方式控制横向和纵向?
- 17. 在纵向和横向模式下为iPad设置不同约束
- 18. 的XCode:如何更改的横向和纵向模式
- 19. Android的横向和纵向模式变化
- 20. 如何检查appdelegate上的横向和纵向模式?
- 21. 纵向和横向居中
- 22. 如何从纵向模式更改为横向模式并锁定它?
- 23. 如何在横向和纵向模式下开始相同的活动?
- 24. 如何在纵向和横向模式下在单个列表视图中显示不同的列数android?
- 25. 定位:纵向和横向在Xcode 4.2
- 26. 不同的横向和纵向设计android
- 27. 为纵向和横向模式调整UIImage的大小
- 28. 加载不同的纵向/横向UIViews
- 29. 如何将纵向视图更改为横向模式?
- 30. 如何在代码中处理Android纵向和横向?
感谢您的回复。我现在可以继续。 –